File: tie_example.cpp

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (63 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//  (C) Copyright Jeremy Siek 2000. Permission to copy, use, modify,
//  sell and distribute this software is granted provided this
//  copyright notice appears in all copies. This software is provided
//  "as is" without express or implied warranty, and with no claim as
//  to its suitability for any purpose.
//
// This is an example demonstrating how to use the tie() function.
// The purpose of tie() is to make it easiery to deal with std::pair
// return values.
//
// Contributed by Jeremy Siek
//
// Sample output
//
// 3 successfully inserted.
// 9 was already in the set.
// There were 2 occurrences of 4.

#include <set>
#include <algorithm>
#include <iostream>
// Note: tie() use to live in boost/utility.hpp, but
// not it is part of the more general Boost Tuple Library.
#include <boost/tuple/tuple.hpp>

int
main(int, char*[])
{
  {
    typedef std::set<int> SetT;
    SetT::iterator i;
    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;
}