stl2--Associative containers

Source: Internet
Author: User

Associative containers

The Association container supports the efficient discovery and reading of elements by key (key). Two basic associative containers are the elements of map and Set,map, organized as key-value pairs, indexed by keys, values for stored and read data, set contains a key, and effectively supports queries about the existence of a key.

Introduction: Pair type

#include <utility>

Pair<int,int>p;p.first=5;p.second=6;pair<int,int>p2;p2=make_pair (5,7);p air<int,int>p3 (9,10);


A map is a collection of key-value pairs that can be used as a subscript to get a value. The key type must support the < operator and work correctly.

definition of a Map object

type of map definition

Dereferencing a map iterator will result in a pair type object whose first member holds the value of the Const,second member.

Map Add Member

use subscript to access the map object

When accessed using the subscript (key), if the subscript exists, returns the value associated with the subscript, and if the subscript does not exist, the map container creates a new element for the key and inserts it into the map object.

Count the number of occurrences of each word entered map<string,int>word_count;string word;while (Cin>>word) ++word_count[word];

Insert uses

M.insert (e)

E is a value_type type on M, if the key does not exist, insert the value into the map and if the key exists (a given key in the map corresponds to only one element), no action is made. The function returns a pair object that points to the map iterator of the element with the key E.first (that is, the inserted element), and a bool type object that indicates whether the element was inserted.

Word_count.insert (Map<string,int>::value_type ("Anna"), Word_count.insert ("Lily", 100);// You can simplify the TypeDef map<string,int>::value_type valtype;//with Make_pair by using a typedef to simplify Word_count.insert (Valtype ("Anna", 50) );

M.insert (beg,end)

Beg and end are iterators that mark the scope of an element, where the element must be of type M.value_type, and for all elements of that range, if his key is not in M, the key and its associated value are inserted into m and the void type is returned.

M.insert (Iter,e)

E is a value of type value_type on M, if the key is not in M, a new element is created, and the iterator, iter, is searched for the location where the new element is stored, returning an iterator to the element with the given key in M.

Finding and reading elements in a map

Delete objects in map

Traversal of Map objects

Map also provides a begin and end operation to generate iterators for traversing the entire container.


Set is simply a collection of keys that support most operations of map definition, including map constructors, insert operations, count, and find,erase operations, in addition to not supporting subscript operators and not defining mapped_type types. As with map, the keys stored by the Set container must also be unique and cannot be modified.

Multimap and Mutiset

Multimap and Mutiset allow a single key to correspond to multiple instances and are included in the same header file as the set and map.

a version of erase with a key parameter removes all elements that have the key and returns the number of deletes, while the version with one or two iterator parameters removes only the specified element and returns void. because values of the same key value are stored in adjacent locations, the following methods can be traversed:1. Use Count to find out the number of times a key appears, and then use the find operation to return an iterator, looking in turn;2. You can use an associative container operation that returns an iterator

the first two functions that are raised in the same key will produce an iterator range that indicates all the elements associated with the key. The iterator returned by lower_bound points to the first instance associated with the key, and the iterator returned by upper_bound points to the next position of the last instance. If the key is not in the container, both operations return the same iterator, pointing to the position in which the key should be inserted according to the order of the elements, or to where the container exceeds the end.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

stl2--Associative containers

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