Usage of C ++ Primer notes in related containers

Source: Internet
Author: User

Associated container

Associated containers support efficient searching and reading of Elements through keys. The two basic associated container types are map and set. Map elements are organized in the form of key-value pairs: Keys are used as the index of elements in map, while values represent the stored and read data. Set only contains one key, and effectively supports queries on whether a key exists. A set or map object cannot add a second element to the same key. If a key must correspond to multiple instances, the multimap or mutiset type is required. These two types allow multiple elements to have the same key.

Pair type:Defined in the header file utility.

Pair creation and use:

Copy codeThe Code is as follows: # include <utility>
Pair <string, int> author ("Peter", 30 );
Cout <author. first <"\ t" <author. second <endl; // you can directly access data members.
// Use typedef to simplify
Typedef pair <string, string> Student;
Student s1, s2 ("aaa", "bbb ");
S1.first = "ccc ";
S1.second = "ddd ";
// Use the make_pair function to generate a new pair object
String first = "eee", second = "fff ";
Student s3 = make_pair (first, second );

Map type:Map is a set of key-value pairs.

Map <K, V>: Type of the key used for indexing in map

Map <K, V >:: mapped_type is used as the associated value type in map.

Map <K, V >:: value_type: A pair type

Objects of the pair type will be generated when the map iterator is unreferenced:

Copy codeThe Code is as follows: map <string, int >:: iterator map_it = word_count.begin ();
Cout <map_it-> first <"" <map_it-> second <endl;

Use subscript to access the map object:

There are two methods to add a key-value pair. You can use the insert member, or use the subscript operator to obtain elements and assign values to the obtained elements.

Accessing map by subscript is different from accessing an array or vector by subscript. Accessing nonexistent elements by subscript will lead to adding a new element to the map container, its key is the value of this submark.

Method 1:

Copy codeThe Code is as follows: map <string, int> word_count;
Word_count ["Peter"] = 10; // equivalent to adding a key-Value Pair
// Create a map object to record the occurrences of each word, which is very concise.
Map <string, int> word_count;
String word;
While (cin> word)
{
++ Word_count [word];
}

Method 2: Use insert:Copy codeThe Code is as follows: map <string, int> word_count;
Word_count.insert (map <string, int>: value_type ("aaa", 1 ));
// Use the insert method to override the word statistics program
Map <string, int> word_count;
String word;
While (cin> word)
{
Pair <map <string, int >:: iterator, bool> ret = word_count.insert (make_pair <string, int> (word, 1 ));
If (! Ret. second) // If the insertion is not successful, it indicates that a key value already exists. The statistical value is + 1
{
+ Ret. first-> second; // first is an iterator pointing to the inserted key.
}
}

Find and read the elements in the map:

Using the subscript operator is a relatively simple method, but this method has a side effect, that is, when the key is not in the map container, the subscript operation inserts a new element with the key.

The map container provides two operations: count and find.

M. count (k) returns the number of k occurrences in m. For map objects, it can only be 1 or 0, and for mutimap containers, more values may appear.

M. find (k) returns the iterator returned by k Index

The count method is used to find the existence of the specified key in the map, and the find method is suitable for finding the elements corresponding to the specified key in the map container.

Copy codeThe Code is as follows: // read the element without inserting a new element.
Int occurs;
Map <string, int >:: iterator it = word_count.find ("foobar"); // if it does not exist, the end iterator is returned.
If (it! = Word_count.end () // it may not be found.
{
Occurs = it. second;
}

Delete an element from a map object:

M. erase (k) deletes the elements whose key is k. The returned value is the number of deleted elements. For map containers, the value must be 0 or 1.

M. erase (p) removes the element pointed to by iterator p from m. The return value is of the void type.

M. erase (B, e) removes an element from m from a pair of iterators. The return value is of the void type.

Iterative traversal of map objects:

Copy codeThe Code is as follows: map <string, int> word_count;
Word_count ["aaa"] = 1;
Word_count ["bbb"] = 2;
Word_count ["ccc"] = 3;
Map <string, int >:: const_iterator iter = word_count.begin ();
While (iter! = Word_count.end ())
{
Cout <iter-> second <endl;
Iter ++;
}

Set Type:

The map container is a set of key-value pairs, while the set container is only a set of simple keys. When you only want to know whether a value exists, using the set container is the most appropriate.

Add an element to set:

Copy codeThe Code is as follows: set <int> set1;
Pair <set <int >:: iterator, bool> p = set1.insert (1); // returns a pair-type object that contains an iterator and a Boolean value.
Set1.insert (2 );
Int arr [] = {1, 2, 3 };
Set <int> set2;
Set2.insert (arr, arr + 3); // return the void type.

Get elements from set: similar to map, use the find and count functions.

Multimap and multiset types:

In map and set containers, a key can only correspond to one instance. The multimap and multiset types allow one key to correspond to multiple instances. The operations supported by multiply are the same as those of map and set, except that multiply does not support subscript operations.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.