C ++ primer associated container

Source: Internet
Author: User

Pair <T1, T2> P1;

Pair <T1, T2> P1 (V1, V2); // V1 is T1, V2 is T2

Make_pair (V1, V2); // return a pair object

P. first;

P. Second;

The associated container cannot be defined by container size or resize.

Map <K, V> M (m2 );

Map <K, V> m;

Map <K, V> M (B, E); // B, e is the map iterator.

Key must support <.

Map <K, V >:: value_type is pair type const key_type mapped_type

Map <K, V >:: mapped_type is the value type associated with the key.

Map <K, V >:: key_type is the key type.

 

MAP is easy to add elements. Similar to subscript.

For example, Map <string, int> word;

Word ["A"] = 1; // insert (make_pair ("A", 1 );

Word ["A"] returns an association value.

Therefore, map can be used to easily record the number of times a word appears.

Map <string, int> word_count;

String word;

While (CIN> word)

{

Word_count [word] ++;

}

 

Insert of Map

M. insert (E); // For pair, an iterator of E and bool are returned to determine whether the insert is successful.

M. insert (beg, end); // return void

M. insert (ITER, e); // ITER is the aid, returns the iterator, pointing to elements with specific keys in m

 

M. Count (k); // returns the number of K occurrences in M.

M. Find (k); // whether the element indexed by K exists. The element iterator is returned.

 

M. Erase (k); // delete an element whose key is K. The size_type type is returned, indicating the number of deleted elements.

M. Erase (p); // Delete the element referred to by iterator P and return void

M. Erase (B, E); // Delete the elements of B and E and return void.

 

Map can be used as word conversion, phone book, and dictionary.

 

Set does not support subscript operators,

Insert (key); // return pair <iterator, bool>;

 

Set can help map to record the word exclusion set.

 

Multimap does not support subscript operations.

Insert always adds elements. Erase deletes all matching elements and returns the number of deleted elements.

In multimap, the elements associated with a key are inevitably stored adjacent to each other. Therefore, after finding the first key, all elements can be found as long as the iterator ++.

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.