1*36ac495dSmrg<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2*36ac495dSmrg<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Pairs</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><meta name="keywords" content="ISO C++, library" /><meta name="keywords" content="ISO C++, runtime, library" /><link rel="home" href="../index.html" title="The GNU C++ Library" /><link rel="up" href="utilities.html" title="Chapter 6. Utilities" /><link rel="prev" href="utilities.html" title="Chapter 6. Utilities" /><link rel="next" href="memory.html" title="Memory" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Pairs</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="utilities.html">Prev</a> </td><th width="60%" align="center">Chapter 6. 3*36ac495dSmrg Utilities 4*36ac495dSmrg 5*36ac495dSmrg</th><td width="20%" align="right"> <a accesskey="n" href="memory.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="std.util.pairs"></a>Pairs</h2></div></div></div><p>The <code class="code">pair<T1,T2></code> is a simple and handy way to 6*36ac495dSmrg carry around a pair of objects. One is of type T1, and another of 7*36ac495dSmrg type T2; they may be the same type, but you don't get anything 8*36ac495dSmrg extra if they are. The two members can be accessed directly, as 9*36ac495dSmrg <code class="code">.first</code> and <code class="code">.second</code>. 10*36ac495dSmrg </p><p>Construction is simple. The default ctor initializes each member 11*36ac495dSmrg with its respective default ctor. The other simple ctor, 12*36ac495dSmrg </p><pre class="programlisting"> 13*36ac495dSmrg pair (const T1& x, const T2& y); 14*36ac495dSmrg </pre><p>does what you think it does, <code class="code">first</code> getting <code class="code">x</code> 15*36ac495dSmrg and <code class="code">second</code> getting <code class="code">y</code>. 16*36ac495dSmrg </p><p>There is a constructor template for copying pairs of other types: 17*36ac495dSmrg </p><pre class="programlisting"> 18*36ac495dSmrg template <class U, class V> pair (const pair<U,V>& p); 19*36ac495dSmrg </pre><p>The compiler will convert as necessary from U to T1 and from 20*36ac495dSmrg V to T2 in order to perform the respective initializations. 21*36ac495dSmrg </p><p>The comparison operators are done for you. Equality 22*36ac495dSmrg of two <code class="code">pair<T1,T2></code>s is defined as both <code class="code">first</code> 23*36ac495dSmrg members comparing equal and both <code class="code">second</code> members comparing 24*36ac495dSmrg equal; this simply delegates responsibility to the respective 25*36ac495dSmrg <code class="code">operator==</code> functions (for types like MyClass) or builtin 26*36ac495dSmrg comparisons (for types like int, char, etc). 27*36ac495dSmrg </p><p> 28*36ac495dSmrg The less-than operator is a bit odd the first time you see it. It 29*36ac495dSmrg is defined as evaluating to: 30*36ac495dSmrg </p><pre class="programlisting"> 31*36ac495dSmrg x.first < y.first || 32*36ac495dSmrg ( !(y.first < x.first) && x.second < y.second ) 33*36ac495dSmrg </pre><p>The other operators are not defined using the <code class="code">rel_ops</code> 34*36ac495dSmrg functions above, but their semantics are the same. 35*36ac495dSmrg </p><p>Finally, there is a template function called <code class="function">make_pair</code> 36*36ac495dSmrg that takes two references-to-const objects and returns an 37*36ac495dSmrg instance of a pair instantiated on their respective types: 38*36ac495dSmrg </p><pre class="programlisting"> 39*36ac495dSmrg pair<int,MyClass> p = make_pair(4,myobject); 40*36ac495dSmrg </pre></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="utilities.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="utilities.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="memory.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 6. 41*36ac495dSmrg Utilities 42*36ac495dSmrg 43*36ac495dSmrg </td><td width="20%" align="center"><a accesskey="h" href="../index.html">Home</a></td><td width="40%" align="right" valign="top"> Memory</td></tr></table></div></body></html>