mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 23:14:02 +00:00
138 lines
3.9 KiB
HTML
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 <class A, class B>
|
|
tied<A,B> tie(A& a, B& b);
|
|
</PRE>
|
|
|
|
<P>
|
|
This is a utility function that makes it more convenient to work with
|
|
a function which returns a std::pair<>. 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ärvi's Binders [<A
|
|
HREF="../graph/doc/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<vertex_iterator,vertex_iterator></TT>. The
|
|
pair of iterators is assigned to the iterator variables <TT>i</TT> and
|
|
<TT>end</TT>.
|
|
|
|
<P>
|
|
<PRE>
|
|
graph_traits< adjacency_list<> >::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/tech/stl/set.html"><TT>std::set</TT></a>.
|
|
|
|
<P>
|
|
<PRE>
|
|
#include <set>
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <boost/utility.hpp>
|
|
|
|
int
|
|
main(int, char*[])
|
|
{
|
|
{
|
|
typedef std::set<int> 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<iterator,bool>
|
|
|
|
int new_vals[2] = { 3, 9 };
|
|
|
|
for (int k = 0; k < 2; ++k) {
|
|
boost::tie(i,inserted) = s.insert(new_vals[k]);
|
|
if (!inserted)
|
|
std::cout << *i << " was already in the set." << std::endl;
|
|
else
|
|
std::cout << *i << " successfully inserted." << 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<iterator,iterator>
|
|
|
|
boost::tie(i,end) = std::equal_range(vals, vals + 6, 4);
|
|
std::cout << "There were " << std::distance(i,end)
|
|
<< " occurrences of " << *i << "." << 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 © 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>
|