<cliext/utility> (STL/CLR)

包括 STL/CLR 标头 <cliext/utility>,以定义类模板 pair 和多个支持函数模板。

语法

#include <cliext/utility>

要求

标头:<cliext/utility>

命名空间:cliext

声明

说明
pair 包装一对元素。
运算符 说明
operator==(配对) pair 等于比较。
operator!=(配对) pair 不等于比较。
operator<(配对) pair 小于比较。
operator<=(配对) pair 小于或等于比较。
operator>(配对) pair 大于比较。
operator>=(配对) pair 大于或等于比较。
函数 说明
make_pair 由一对值组成一个 pair

pair

模板类描述了一个用于包装一对值的对象。

语法

template<typename Value1,
    typename Value2>
    ref class pair;

参数

Value1
第一个包装值的类型。

Value2
第二个包装值的类型。

成员

类型定义 说明
pair::first_type 第一个包装值的类型。
pair::second_type 第二个包装值的类型。
成员对象 说明
pair::first 第一个存储值。
pair::second 第二个存储值。
成员函数 说明
pair::pair 构造 pair 对象。
pair::swap 交换两个 pair 对象的内容。
操作员 说明
pair::operator= 替换存储的值对。

注解

对象存储一对值。 你可以使用此模板类将两个值组合为一个对象。 此外,对象 cliext::pair(此处描述)仅存储托管类型;要存储一对非托管类型,请使用在 <utility> 中声明的 std::pair

pair::first

第一个包装值。

语法

Value1 first;

备注

该对象存储第一个包装值。

示例

// cliext_pair_first.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);

    cliext::pair<wchar_t, int>::first_type first_val = c1.first;
    cliext::pair<wchar_t, int>::second_type second_val = c1.second;
    System::Console::WriteLine("[{0}, {1}]", first_val, second_val);
    return (0);
    }
[x, 3]

pair::first_type

第一个包装值的类型。

语法

typedef Value1 first_type;

备注

该类型是模板参数 Value1 的同义词。

示例

// cliext_pair_first_type.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);

    cliext::pair<wchar_t, int>::first_type first_val = c1.first;
    cliext::pair<wchar_t, int>::second_type second_val = c1.second;
    System::Console::WriteLine("[{0}, {1}]", first_val, second_val);
    return (0);
    }
[x, 3]

pair::operator=

替换存储的值对。

语法

pair<Value1, Value2>% operator=(pair<Value1, Value2>% right);

参数

right
pair 用于复制。

备注

成员运算符将 right 复制到对象,然后返回 *this。 你可以使用此类将存储的值对替换为 right 中存储的值对的副本。

示例

// cliext_pair_operator_as.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);

// assign to a new pair
    cliext::pair<wchar_t, int> c2;
    c2 = c1;
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);
    return (0);
    }
[x, 3]
[x, 3]

pair::pair

构造 pair 对象。

语法

pair();
pair(pair<Coll>% right);
pair(pair<Coll>^ right);
pair(Value1 val1, Value2 val2);

参数

right
pair 用于存储。

val1
要存储的第一个值。

val2
要存储的第二个值。

备注

构造函数:

pair();

使用默认构造值初始化存储的对。

构造函数:

pair(pair<Value1, Value2>% right);

使用 right.firstright.second 初始化存储的对。

pair(pair<Value1, Value2>^ right);

使用 right->firstright->second 初始化存储的对。

构造函数:

pair(Value1 val1, Value2 val2);

使用 val1val2 初始化存储的对。

示例

// cliext_pair_construct.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
// construct an empty container
    cliext::pair<wchar_t, int> c1;
    System::Console::WriteLine("[{0}, {1}]",
        c1.first == L'\0' ? "\\0" : "??", c1.second);

// construct with a pair of values
    cliext::pair<wchar_t, int> c2(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

// construct by copying another pair
    cliext::pair<wchar_t, int> c3(c2);
    System::Console::WriteLine("[{0}, {1}]", c3.first, c3.second);

// construct by copying a pair handle
    cliext::pair<wchar_t, int> c4(%c3);
    System::Console::WriteLine("[{0}, {1}]", c4.first, c4.second);

    return (0);
    }
[\0, 0]
[x, 3]
[x, 3]
[x, 3]

pair::second

第二个包装值。

语法

Value2 second;

备注

该对象存储第二个包装值。

示例

// cliext_pair_second.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);

    cliext::pair<wchar_t, int>::first_type first_val = c1.first;
    cliext::pair<wchar_t, int>::second_type second_val = c1.second;
    System::Console::WriteLine("[{0}, {1}]", first_val, second_val);
    return (0);
    }
[x, 3]

pair::second_type

第二个包装值的类型。

语法

typedef Value2 second_type;

备注

该类型是模板参数 Value2 的同义词。

示例

// cliext_pair_second_type.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);

    cliext::pair<wchar_t, int>::first_type first_val = c1.first;
    cliext::pair<wchar_t, int>::second_type second_val = c1.second;
    System::Console::WriteLine("[{0}, {1}]", first_val, second_val);
    return (0);
    }
[x, 3]

pair::swap

交换两个 pair 对象的内容。

语法

void swap(pair<Value1, Value2>% right);

参数

right
pair 用于交换内容。

备注

成员函数在 *thisright 之间交换存储的值对。

示例

// cliext_pair_swap.cpp
// compile with: /clr
#include <cliext/adapter>
#include <cliext/deque>

typedef cliext::collection_adapter<
    System::Collections::ICollection> Mycoll;
int main()
    {
    cliext::deque<wchar_t> d1;
    d1.push_back(L'a');
    d1.push_back(L'b');
    d1.push_back(L'c');
    Mycoll c1(%d1);

// display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

// construct another container with repetition of values
    cliext::deque<wchar_t> d2(5, L'x');
    Mycoll c2(%d2);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

// swap and redisplay
    c1.swap(c2);
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
x x x x x
x x x x x
a b c

make_pair

由一对值组成一个 pair

语法

template<typename Value1,
    typename Value2>
    pair<Value1, Value2> make_pair(Value1 first, Value2 second);

参数

Value1
第一个包装值的类型。

Value2
第二个包装值的类型。

first
要包装的第一个值。

second
要包装的第二个值。

备注

函数模板返回 pair<Value1, Value2>(first, second)。 可使用它从一对值构造一个 pair<Value1, Value2> 对象。

示例

// cliext_make_pair.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);

    c1 = cliext::make_pair(L'y', 4);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    return (0);
    }
[x, 3]
[y, 4]

operator!=(配对)

pair 不等于比较。

语法

template<typename Value1,
    typename Value2>
    bool operator!=(pair<Value1, Value2>% left,
        pair<Value1, Value2>% right);

参数

left
要比较的左 pair

right
要比较的右 pair

备注

运算符函数返回 !(left == right)。 当两个 pair 对象按元素逐个比较时,可以用它来测试 left 的顺序是否与 right 不同。

示例

// cliext_pair_operator_ne.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    cliext::pair<wchar_t, int> c2(L'x', 4);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

    System::Console::WriteLine("[x 3] != [x 3] is {0}",
        c1 != c1);
    System::Console::WriteLine("[x 3] != [x 4] is {0}",
        c1 != c2);
    return (0);
    }
[x, 3]
[x, 4]
[x 3] != [x 3] is False
[x 3] != [x 4] is True

operator<

pair 小于比较。

语法

template<typename Value1,
    typename Value2>
    bool operator<(pair<Value1, Value2>% left,
        pair<Value1, Value2>% right);

参数

left
要比较的左 pair

right
要比较的右 pair

备注

运算符函数返回 left.first < right.first || !(right.first < left.first && left.second < right.second。 当两个 pair 对象按元素逐个比较时,可以用它来测试 left 的顺序是否在 right 前面。

示例

// cliext_pair_operator_lt.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    cliext::pair<wchar_t, int> c2(L'x', 4);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

    System::Console::WriteLine("[x 3] < [x 3] is {0}",
        c1 < c1);
    System::Console::WriteLine("[x 3] < [x 4] is {0}",
        c1 < c2);
    return (0);
    }
[x, 3]
[x, 4]
[x 3] < [x 3] is False
[x 3] < [x 4] is True

operator<=

pair 小于或等于比较。

语法

template<typename Value1,
    typename Value2>
    bool operator<=(pair<Value1, Value2>% left,
        pair<Value1, Value2>% right);

参数

left
要比较的左 pair

right
要比较的右 pair

备注

运算符函数返回 !(right < left)。 当两个 pair 对象按元素逐个比较时,可以用它来测试 left 的顺序是否不在 right 后面。

示例

// cliext_pair_operator_le.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    cliext::pair<wchar_t, int> c2(L'x', 4);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

    System::Console::WriteLine("[x 3] <= [x 3] is {0}",
        c1 <= c1);
    System::Console::WriteLine("[x 4] <= [x 3] is {0}",
        c2 <= c1);
    return (0);
    }
[x, 3]
[x, 4]
[x 3] <= [x 3] is True
[x 4] <= [x 3] is False

operator==

pair 等于比较。

语法

template<typename Value1,
    typename Value2>
    bool operator==(pair<Value1, Value2>% left,
        pair<Value1, Value2>% right);

参数

left
要比较的左 pair

right
要比较的右 pair

备注

运算符函数返回 left.first == right.first && left.second == right.second。 当两个 pair 对象按元素逐个比较时,可以用它来测试 left 的顺序是否与 right 相同。

示例

// cliext_pair_operator_eq.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    cliext::pair<wchar_t, int> c2(L'x', 4);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

    System::Console::WriteLine("[x 3] == [x 3] is {0}",
        c1 == c1);
    System::Console::WriteLine("[x 3] == [x 4] is {0}",
        c1 == c2);
    return (0);
    }
[x, 3]
[x, 4]
[x 3] == [x 3] is True
[x 3] == [x 4] is False

pair::operator>

pair 大于比较。

语法

template<typename Value1,
    typename Value2>
    bool operator>(pair<Value1, Value2>% left,
        pair<Value1, Value2>% right);

参数

left
要比较的左 pair

right
要比较的右 pair

注解

运算符函数返回 right < left。 当两个 pair 对象按元素逐个比较时,可以用它来测试 left 的顺序是否在 right 后面。

示例

// cliext_pair_operator_gt.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    cliext::pair<wchar_t, int> c2(L'x', 4);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

    System::Console::WriteLine("[x 3] > [x 3] is {0}",
        c1 > c1);
    System::Console::WriteLine("[x 4] > [x 3] is {0}",
        c2 > c1);
    return (0);
    }
[x, 3]
[x, 4]
[x 3] > [x 3] is False
[x 4] > [x 3] is True

operator>=

pair 大于或等于比较。

语法

template<typename Value1,
    typename Value2>
    bool operator>=(pair<Value1, Value2>% left,
        pair<Value1, Value2>% right);

参数

left
要比较的左 pair

right
要比较的右 pair

备注

运算符函数返回 !(left < right)。 当两个 pair 对象按元素逐个比较时,可以用它来测试 left 的顺序是否不在 right 前面。

示例

// cliext_pair_operator_ge.cpp
// compile with: /clr
#include <cliext/utility>

int main()
    {
    cliext::pair<wchar_t, int> c1(L'x', 3);
    System::Console::WriteLine("[{0}, {1}]", c1.first, c1.second);
    cliext::pair<wchar_t, int> c2(L'x', 4);
    System::Console::WriteLine("[{0}, {1}]", c2.first, c2.second);

    System::Console::WriteLine("[x 3] >= [x 3] is {0}",
        c1 >= c1);
    System::Console::WriteLine("[x 3] >= [x 4] is {0}",
        c1 >= c2);
    return (0);
    }
[x, 3]
[x, 4]
[x 3] >= [x 3] is True
[x 3] >= [x 4] is False