utility/tie.html
Beman Dawes c5915c23e7 Fix broken link
[SVN r7870]
2000-09-28 17:47:29 +00:00

138 lines
3.9 KiB
HTML

<HTML>
<!--
-- Copyright (c) Jeremy Siek, Lie-Quan Lee, and Andrew Lumsdaine 2000
--
-- Permission to use, copy, modify, distribute and sell this software
-- and its documentation for any purpose is hereby granted without fee,
-- provided that the above copyright notice appears in all copies and
-- that both that copyright notice and this permission notice appear
-- in supporting documentation. We make no
-- representations about the suitability of this software for any
-- purpose. It is provided "as is" without express or implied warranty.
-->
<Head>
<Title>Boost Tie</Title>
<BODY BGCOLOR="#ffffff" LINK="#0000ee" TEXT="#000000" VLINK="#551a8b"
ALINK="#ff0000">
<IMG SRC="../../c++boost.gif"
ALT="C++ Boost" width="277" height="86">
<BR Clear>
<H1><A NAME="sec:tie"></A>
<TT>tie</TT>
</H1>
<P>
<PRE>
template &lt;class A, class B&gt;
tied&lt;A,B&gt; tie(A&amp; a, B&amp; b);
</PRE>
<P>
This is a utility function that makes it more convenient to work with
a function which returns a std::pair&lt;&gt;. The effect of the <TT>tie()</TT>
function is to allow the assignment of the two values of the pair to
two separate variables. The idea for this comes from Jaakko
J&#228;rvi's Binders&nbsp;[<A
HREF="../graph/docs/bibliography.html#jaakko_tuple_assign">1</A>].
<P>
<H3>Where Defined</H3>
<P>
<a href="../../boost/utility.hpp"><TT>boost/utility.hpp</TT></a>
<P>
<H3>Example</H3>
<P>
An example of using the <TT>tie()</TT> function with the
<TT>vertices()</TT> function, which returns a pair of
type <TT>std::pair&lt;vertex_iterator,vertex_iterator&gt;</TT>. The
pair of iterators is assigned to the iterator variables <TT>i</TT> and
<TT>end</TT>.
<P>
<PRE>
graph_traits&lt; adjacency_list&lt;&gt; &gt;::vertex_iterator i, end;
for(tie(i,end) = vertices(G); i != end; ++i)
// ...
</PRE>
<P>
Here is another example that uses <TT>tie()</TT> for handling operations with <a
href="http://www.sgi.com/Technology/STL/set.html"><TT>std::set</TT></a>.
<P>
<PRE>
#include &lt;set&gt;
#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;boost/utility.hpp&gt;
int
main(int, char*[])
{
{
typedef std::set&lt;int&gt; SetT;
SetT::iterator i, end;
bool inserted;
int vals[5] = { 5, 2, 4, 9, 1 };
SetT s(vals, vals + 5);
// Using tie() with a return value of pair&lt;iterator,bool&gt;
int new_vals[2] = { 3, 9 };
for (int k = 0; k &lt; 2; ++k) {
boost::tie(i,inserted) = s.insert(new_vals[k]);
if (!inserted)
std::cout &lt;&lt; *i &lt;&lt; &quot; was already in the set.&quot; &lt;&lt; std::endl;
else
std::cout &lt;&lt; *i &lt;&lt; &quot; successfully inserted.&quot; &lt;&lt; std::endl;
}
}
{
int* i, *end;
int vals[6] = { 5, 2, 4, 4, 9, 1 };
std::sort(vals, vals + 6);
// Using tie() with a return value of pair&lt;iterator,iterator&gt;
boost::tie(i,end) = std::equal_range(vals, vals + 6, 4);
std::cout &lt;&lt; &quot;There were &quot; &lt;&lt; std::distance(i,end)
&lt;&lt; &quot; occurrences of &quot; &lt;&lt; *i &lt;&lt; &quot;.&quot; &lt;&lt; std::endl;
// Footnote: of course one would normally just use std::count()
// to get this information, but that would spoil the example :)
}
return 0;
}
</PRE>
The output is:
<PRE>
3 successfully inserted.
9 was already in the set.
There were 2 occurrences of 4.
</PRE>
<br>
<HR>
<TABLE>
<TR valign=top>
<TD nowrap>Copyright &copy 2000</TD><TD>
<A HREF=http://www.boost.org/people/jeremy_siek.htm>Jeremy Siek</A>,
Univ.of Notre Dame (<A
HREF="mailto:jsiek@lsc.nd.edu">jsiek@lsc.nd.edu</A>)<br>
<A HREF=http://www.lsc.nd.edu/~llee1>Lie-Quan Lee</A>, Univ.of Notre Dame (<A HREF="mailto:llee1@lsc.nd.edu">llee1@lsc.nd.edu</A>)<br>
<A HREF=http://www.lsc.nd.edu/~lums>Andrew Lumsdaine</A>,
Univ.of Notre Dame (<A
HREF="mailto:lums@lsc.nd.edu">lums@lsc.nd.edu</A>)
</TD></TR></TABLE>
</BODY>
</HTML>