The basics of programming in c++ for beginners

STL containers: multimap. Part 8

We have considered a simple example of using map<> to count the occurrences of individual characters in the text. For this purpose, we used a container map<>. But the library STL provides us and other (close) type of container - it multimap<>, which allows for the presence of many elements (pair<>) in its composition to the the same key values.

Naturally, that the basic rules of operation multimap<> change (compared with map<>). For such a container will have the following differences in the behavior of:

  • It contains ordered pairs <key,value>, where the key and value may belong to arbitrary types;

  • Elements with any key values ​​must not be uniqueme, in the ordered sequence of elements (by key) such equivalent elements are, how different elements, and they are arranged one after the other;

  • Since the keys are the same, then the operation of adding a new pair in the table (method insert()) always successful. No need to return the result of such operation: return value - void;

  • Since it is now in the container can be a lot of elements with equal keys, it introduced an additional method count(). It gets the value of the key parameter, returns the number of occurrences of the elements, have a key, into the container;

  • removal operations (method erase()) with an indication of the element to remove the key deletes all at once elements with the same key;

Let's see how multimap<> cope with the previous task. That text files for testing program:

30  Brother_And_Sister11  Humpty-Dumpty34  Jabberwock

As in the previous example, It uses several bulky to enter characters from the stream, contains the text in many strings. This is because, what and). I would like to read and also space characters as symbols, rather than as delimiters and b). you could just read characters from the series cin followed by excluding line breaks. But in the illustrated embodiment, it would be desirable to preserve the independence of the operating system (distinguish the so-called DOS and UNIX line feed). The rest of the code has become a lot shorter and easier.

Result of, that we got, shown below (next repeated to compare the same action, performed map<>):

multimap c ++ class, stl containers, multimap for beginners
multimap
class map c ++, stl containers, map for beginners
map

We draw attention to the fact, as the removal of a single operator alphabet.erase( ‘a’ ) we have removed from the tables 38 elements, Identifying keys ‘a’. Where did the size size() in 696 elements?

multimap c ++ class, stl containers, multimap for beginners

(Show shape wc command calculates the Linux operating system the number of rows in file. The ls command outputs length file, i.e.. the number of individual bytes, characters in the file. In Windows, you can try to achieve a similar result using the dir command.)

If we have the total number of symbols (length in bytes) subtract the number of lines (line breaks) in file, then we get this figure: 726 – 30 = 696. In this way, for each input symbol has been established separate table element, for example, key ‘s’ It was 39 such elements.

All, previously said about map container<> (except premises and sampling algorithm) fully applies to multimap<>, for example, comparability requirement and the key, how to define or redefine the comparison operation.

Leave a Reply

Your email address will not be published. Required fields are marked *