# STL containers: set and multiset. Part 9

STL containers: set and multiset. Part 9

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:

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):

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.