Quite often, in practice, it is required to monitor only those belonging to other facilities or to a subset. Such collections in classical mathematics are called a bunch of, to which a particular object may be owned or, or not. And the STL provides such kind of container, which is called - set<> (a bunch of).
Each container element set<> It contains only key value (not a couple, as for map<>), so this type of container is very effective in implementing the operation check in a set of values. This container provides many common methods in comparison с map<>, although here are some degenerate quality (and sometimes useless):
Paste operation (method insert()) the new key value to the set of returns pair type <iterator, bool> (just, as for map<>). In this pair of second component (second, bool type) points to the success of the operation: if this true, the first component of the return value (first) iterator gives a new added element. But it is quite meaningless return value: if returned true, the new value is successfully added to the set, if returned false, it is already present in previously set - in both cases the final state set will be identical (however, in practice, the return value insert() usually not check ... and many people do not know, that there is generally provided for the return value at all);
method is implemented for the container count() (as for multimap<>), but, as the key value may be present in the set only in one copy, method count() can only return 0, if this value is missing, and 1 When a value is present.
implemented method for container found(), which returns iterator elements, if the value is found, and the value of the iterator end() if the value is not in the set.
To demonstrate the above will create an application, is N times (parameter, defined in the application startup command) loop generates a random number in a fixed range [0…lim] and puts it in the set. Clear, that when N increases more than lim (and furthermore at N much More lim), each number range[0…lim] It will generate a greater and greater number of times:
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 | #include <iostream> #include <cstdlib> #include <iomanip> #include <cmath> #include <set> using namespace std; int main( int argc, char **argv ) { unsigned lim = 30, n = argc > 1 ? atoi( argv[ 1 ] ) : lim; set<unsigned> rnd; for( unsigned i = 0; i < n; i++ ) { rnd.insert( (unsigned)nearbyint( (float)rand() / RAND_MAX * ( lim - 1 ) ) ); } cout.fill( '0' ); for( unsigned i = 0; i < lim; i++ ) { if( 1 == rnd.count( i ) ) cout << setw( 2 ) << i << " "; else cout << "- "; } cout << endl; for( auto i = rnd.begin(); i != rnd.end(); i++ ) cout << setw( 2 ) << *i << " "; cout << endl; } |
Another type of container is close multiset<>, allowing each key value be unique, and be in the set of any number of times. These two containers (set<> and multiset<>) so similar, that in the application shown above, we will replace all 2 strings (method count() for multiset<> returns the number of occurrences values in the set):
1 2 3 4 | ... multiset<unsigned> rnd; ... if( rnd.count( i ) > 0 ) cout << setw( 2 ) << i << " "; |
But the application's behavior changes radically (for comparison shows results of a number of 2 applications under identical conditions):
See, that multiset values 1, 2, 13, 17… no container, and the value 18, for example, present there 5 times.