Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
multiset.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for set_intersection(), set_union()
30#include <iostream> // for cout, endl, ostream
31#include <iterator> // for ostream_iterator
32#include <sstream> // for ostringstream
33#include <set> // for set
34
35#include <examples.h>
36
37
38typedef std::multiset<int, std::less<int>, std::allocator<int> > IntSet;
39
40
41std::ostream& operator<< (std::ostream& out, const IntSet& s)
42{
43 std::ostringstream strm;
44
45 typedef std::ostream_iterator<int, char, std::char_traits<char> >
47
48 std::copy (s.begin (), s.end (), OstreamIter (strm, " "));
49
50 const std::ostream::sentry guard (out);
51
52 if (guard) {
53 const std::string data = strm.str ();
54
55 if ( std::streamsize (data.size ())
56 != out.rdbuf ()->sputn (data.data (), data.size ()))
57 out.setstate (std::ios::badbit);
58 }
59
60 return out;
61}
62
63
64int main ()
65{
66 // Create a multiset of integers.
67 IntSet si;
68
69 for (IntSet::value_type j = 0; j < 2; j++) {
70 for (IntSet::value_type i = 0; i < 10; ++i)
71 // Insert values with a hint.
72 si.insert (si.begin (), i);
73 }
74
75 // Print out the multiset.
76 std::cout << si << std::endl;
77
78 // Make another multiset and an empty multiset.
79 IntSet si2, result;
80 for (IntSet::value_type i = 0; i < 10; i++)
81 si2.insert (i + 5);
82
83 std::cout << si2 << std::endl;
84
85 // Try a couple of set algorithms.
86 std::set_union (si.begin (), si.end (), si2.begin (), si2.end (),
87 std::inserter (result, result.begin ()));
88
89 std::cout << "Union:\n" << result << std::endl;
90
91 result.erase (result.begin (), result.end ());
92
93 std::set_intersection (si.begin (), si.end (), si2.begin (), si2.end (),
94 std::inserter (result, result.begin ()));
95
96 std::cout << "Intersection:\n" << result << std::endl;
97
98 return 0;
99}
#define i
Definition 17.names.cpp:126
#define j
Definition 17.names.cpp:127
#define s
#define strm
Definition 17.names.cpp:549
volatile int result
const char *const data[]
const int out
std::ostream & operator<<(std::ostream &out, const IntSet &s)
Definition multiset.cpp:41
std::multiset< int, std::less< int >, std::allocator< int > > IntSet
Definition multiset.cpp:38
int main()
Definition multiset.cpp:64
std::ostream_iterator< int, char, std::char_traits< char > > OstreamIter
Definition radix.cpp:42