Standard C++ Library Class Reference
// print out the set
cout << sd << endl << endl;
// now let's erase half of the elements in the set
int half = sd.size() >> 1;
set_type::iterator sdi = sd.begin();
advance(sdi,half);
sd.erase(sd.begin(),sdi);
// print it out again
cout << sd << endl << endl;
// Make another set and an empty result set
set_type sd2, sdResult;
for (i = 1; i < 9; i++)
sd2.insert(i+5);
cout << sd2 << endl;
// Try a couple of set algorithms
set_union(sd.begin(),sd.end(),sd2.begin(),sd2.end(),
inserter(sdResult,sdResult.begin()));
cout << "Union:" << endl << sdResult << endl;
sdResult.erase(sdResult.begin(),sdResult.end());
set_intersection(sd.begin(),sd.end(),
sd2.begin(),sd2.end(),
inserter(sdResult,sdResult.begin()));
cout << "Intersection:" << endl << sdResult << endl;
return 0;
}
Output :
0 1 2 3 4 5 6 7 8 9
5 6 7 8 9
6 7 8 9 10 11 12 13
Union:
5 6 7 8 9 10 11 12 13
Intersection:
6 7 8 9
Warnings
Member function templates are used in all containers provided by the Standard Template Library. An
example of this feature is the constructor for set <Key, Compare, Allocator> that takes two templated
iterators:
template <class InputIterator>
set (InputIterator, InputIterator,
const Compare& = Compare(),
const Allocator& = Allocator());
set also has an insert function of this type. These functions, when not restricted by compiler
limitations, allow you to use any type of input iterator as arguments. For compilers that do not support
this feature, we provide substitute functions that allow you to use an iterator obtained from the same