Bewerken

Delen via


<cliext/utility> (STL/CLR)

Include the STL/CLR header <cliext/utility> to define the class template pair and several supporting function templates.

Syntax

#include <cliext/utility>

Requirements

Header: <cliext/utility>

Namespace: cliext

Declarations

Class Description
pair Wrap a pair of elements.
Operator Description
operator== (pair) pair equal comparison.
operator!= (pair) pair not equal comparison.
operator< (pair) pair less than comparison.
operator<= (pair) pair less than or equal comparison.
operator> (pair) pair greater than comparison.
operator>= (pair) pair greater than or equal comparison.
Function Description
make_pair Make a pair from a pair of values.

pair

The template class describes an object that wraps a pair of values.

Syntax

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

Parameters

Value1
The type of first wrapped value.

Value2
The type of second wrapped value.

Members

Type definition Description
pair::first_type The type of the first wrapped value.
pair::second_type The type of the second wrapped value.
Member object Description
pair::first The first stored value.
pair::second The second stored value.
Member function Description
pair::pair Constructs a pair object.
pair::swap Swaps the contents of two pair objects.
Operator Description
pair::operator= Replaces the stored pair of values.

Remarks

The object stores a pair of values. You use this template class to combine two values into a single object. Also, the object cliext::pair (described here) stores only managed types; to store a pair of unmanaged types use std::pair, declared in <utility>.

pair::first

The first wrapped value.

Syntax

Value1 first;

Remarks

The object stores the first wrapped value.

Example

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

The type of the first wrapped value.

Syntax

typedef Value1 first_type;

Remarks

The type is a synonym for the template parameter Value1.

Example

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

Replaces the stored pair of values.

Syntax

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

Parameters

right
pair to copy.

Remarks

The member operator copies right to the object, then returns *this. You use it to replace the stored pair of values with a copy of the stored pair of values in right.

Example

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

Constructs a pair object.

Syntax

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

Parameters

right
pair to store.

val1
First value to store.

val2
Second value to store.

Remarks

The constructor:

pair();

initializes the stored pair with default constructed values.

The constructor:

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

initializes the stored pair with right.first and right.second.

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

initializes the stored pair with right->first and right->second.

The constructor:

pair(Value1 val1, Value2 val2);

initializes the stored pair with val1 and val2.

Example

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

The second wrapped value.

Syntax

Value2 second;

Remarks

The object stores the second wrapped value.

Example

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

The type of the second wrapped value.

Syntax

typedef Value2 second_type;

Remarks

The type is a synonym for the template parameter Value2.

Example

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

Swaps the contents of two pair objects.

Syntax

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

Parameters

right
pair to swap contents with.

Remarks

The member function swaps the stored pair of values between *this and right.

Example

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

Make a pair from a pair of values.

Syntax

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

Parameters

Value1
The type of the first wrapped value.

Value2
The type of the second wrapped value.

first
First value to wrap.

second
Second value to wrap.

Remarks

The function template returns pair<Value1, Value2>(first, second). You use it to construct a pair<Value1, Value2> object from a pair of values.

Example

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

pair not equal comparison.

Syntax

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

Parameters

left
Left pair to compare.

right
Right pair to compare.

Remarks

The operator function returns !(left == right). You use it to test whether left isn't ordered the same as right when the two pair objects are compared element by element.

Example

// 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 less than comparison.

Syntax

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

Parameters

left
Left pair to compare.

right
Right pair to compare.

Remarks

The operator function returns left.first < right.first || !(right.first < left.first && left.second < right.second. You use it to test whether left is ordered the before right when the two pair objects are compared element by element.

Example

// 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 less than or equal comparison.

Syntax

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

Parameters

left
Left pair to compare.

right
Right pair to compare.

Remarks

The operator function returns !(right < left). You use it to test whether left isn't ordered after right when the two pair objects are compared element by element.

Example

// 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 equal comparison.

Syntax

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

Parameters

left
Left pair to compare.

right
Right pair to compare.

Remarks

The operator function returns left.first == right.first && left.second == right.second. You use it to test whether left is ordered the same as right when the two pair objects are compared element by element.

Example

// 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 greater than comparison.

Syntax

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

Parameters

left
Left pair to compare.

right
Right pair to compare.

Remarks

The operator function returns right < left. You use it to test whether left is ordered after right when the two pair objects are compared element by element.

Example

// 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 greater than or equal comparison.

Syntax

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

Parameters

left
Left pair to compare.

right
Right pair to compare.

Remarks

The operator function returns !(left < right). You use it to test whether left isn't ordered before right when the two pair objects are compared element by element.

Example

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