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