共用方式為


<cliext/utility> (STL/CLR)

包含 STL/CLR 標頭 <cliext/utility> ,以定義類別範本 pair 和數個支援的函式範本。

語法

#include <cliext/utility>

需求

Header: < 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 第二個包裝值的型別。
Member 物件 描述
pair::first 第一個預存值。
pair::second 第二個預存值。
成員函數 描述
pair::pair 建構 pair 物件。
pair::swap 交換兩 pair 個 物件的內容。
運算子 描述
pair::operator= 取代儲存的值組。

備註

物件會儲存一組值。 您可以使用這個範本類別,將兩個值合併成單一物件。 此外,物件 cliext::pair (這裡所述)只會儲存 Managed 型別;若要儲存一對 Unmanaged 型別,請在 std::pair<utility> 宣告 。

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.second 初始化預存的配對 right.first

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

使用 和 right->second 初始化預存的配對 right->first

建構函式:

pair(Value1 val1, Value2 val2);

使用 和 val2 初始化預存的配對 val1

範例

// 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 以交換內容。

備註

成員函式會在 和 right 之間 *this 交換儲存的值組。

範例

// 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) 回 。 您可以使用它來測試是否 left 與元素比較兩 pair 個物件時的順序不同 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 回 。 您可以使用它來測試是否 left 依 元素比較兩 pair 個物件之前 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 回 。 您可以使用它來測試是否 left 依元素比較兩 pair 個物件時,排序方式與兩個物件的順序相同 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) 回 。 您可以使用它來測試兩 left 個物件是否依元素比較元素之前 rightpair 未排序。

範例

// 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