description | title | ms.date | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: utility (STL/CLR) |
utility (STL/CLR) |
11/04/2016 |
reference |
|
|
fb48cb75-d5ef-47ce-b526-bf60dc86c552 |
Include the STL/CLR header <cliext/utility>
to define the class template pair
and several supporting function templates.
#include <cliext/utility>
Header: <cliext/utility>
Namespace: cliext
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. |
The template class describes an object that wraps a pair of values.
template<typename Value1,
typename Value2>
ref class pair;
Value1
The type of first wrapped value.
Value2
The type of second wrapped value.
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. |
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>
.
The first wrapped value.
Value1 first;
The object stores the first wrapped value.
// 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]
The type of the first wrapped value.
typedef Value1 first_type;
The type is a synonym for the template parameter 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]
Replaces the stored pair of values.
pair<Value1, Value2>% operator=(pair<Value1, Value2>% right);
right
pair
to copy.
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
.
// 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]
Constructs a pair
object.
pair();
pair(pair<Coll>% right);
pair(pair<Coll>^ right);
pair(Value1 val1, Value2 val2);
right
pair
to store.
val1
First value to store.
val2
Second value to store.
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
.
// 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]
The second wrapped value.
Value2 second;
The object stores the second wrapped value.
// 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]
The type of the second wrapped value.
typedef Value2 second_type;
The type is a synonym for the template parameter 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]
Swaps the contents of two pair
objects.
void swap(pair<Value1, Value2>% right);
right
pair
to swap contents with.
The member function swaps the stored pair of values between *this
and right
.
// 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 a pair
from a pair of values.
template<typename Value1,
typename Value2>
pair<Value1, Value2> make_pair(Value1 first, Value2 second);
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.
The function template returns pair<Value1, Value2>(first, second)
. You use it to construct a pair<Value1, Value2>
object from a pair of values.
// 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]
pair
not equal comparison.
template<typename Value1,
typename Value2>
bool operator!=(pair<Value1, Value2>% left,
pair<Value1, Value2>% right);
left
Left pair
to compare.
right
Right pair
to compare.
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.
// 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
pair
less than comparison.
template<typename Value1,
typename Value2>
bool operator<(pair<Value1, Value2>% left,
pair<Value1, Value2>% right);
left
Left pair
to compare.
right
Right pair
to compare.
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.
// 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
pair
less than or equal comparison.
template<typename Value1,
typename Value2>
bool operator<=(pair<Value1, Value2>% left,
pair<Value1, Value2>% right);
left
Left pair
to compare.
right
Right pair
to compare.
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.
// 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
pair
equal comparison.
template<typename Value1,
typename Value2>
bool operator==(pair<Value1, Value2>% left,
pair<Value1, Value2>% right);
left
Left pair
to compare.
right
Right pair
to compare.
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.
// 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
greater than comparison.
template<typename Value1,
typename Value2>
bool operator>(pair<Value1, Value2>% left,
pair<Value1, Value2>% right);
left
Left pair
to compare.
right
Right pair
to compare.
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.
// 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
pair
greater than or equal comparison.
template<typename Value1,
typename Value2>
bool operator>=(pair<Value1, Value2>% left,
pair<Value1, Value2>% right);
left
Left pair
to compare.
right
Right pair
to compare.
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.
// 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