The STL <utility> header file describes a very simple template class pair, which is used to represent a binary group or element pair, it also provides a comparison operator template function for comparing the size of elements in Lexicographic Order.
For example, to define an object to represent a plane coordinate point, you can:
Pair <double, double> P1;
Cin> p1.first> p1.second;
The pair template class requires two parameters: the data type of the first element and the Data Type of the last element. The pair template class object has two members: first and second, indicating the first and last elements respectively.
Six comparison operators on pair have been defined in <utility>: <,>, <=, >=, =, and ,! =, The rule is to compare first and second when first is equal, which conforms to the logic of most applications. Of course, you can also re-specify your own comparison logic by reloading these operators.
In addition to defining a pair object directly, if you need to generate a pair object in real time, you can also call a template function defined in <utility>: make_pair. Make_pair requires two parameters: the first element and the end element of the element pair.
In question 1067 -- uugly numbers, you can use pair to represent the node on the deduction tree and use first to represent the value of the node, second is used to represent the factor that the node is multiplied by the parent node.
# Include <iostream>
# Include <queue>
Using namespace STD;
Typedef pair <unsigned long, int> node_type;
Main ()
{
Unsigned long result [1500];
Priority_queue <node_type
, Vector <node_type
>, Greater <node_type
> Q;
Q. Push (make_pair (1, 2)
);
For (INT I = 0; I <1500; I ++)
{
Node_type node = Q. Top (); q. Pop ();
Switch (node. Second
)
{
Case 2: Q. Push (make_pair (node. First * 2, 2)
);
Case 3: Q. Push (make_pair (node. First * 3, 3)
);
Case 5: Q. Push (make_pair (node. First * 5, 5)
);
}
Result [I] = node. First
;
}
Int N;
Cin> N;
While (n> 0)
{
Cout <result [n-1] <Endl;
Cin> N;
}
Return 1;
} <Utility> looks like a very simple header file, but the design of <utility> reflects the basic idea of STL design. Students who are interested in understanding and studying STL carefully read and understand this simple header file, which is a way to get started.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/camel_flying/archive/2009/08/17/4454172.aspx