C + + associative containers

Source: Internet
Author: User

1. Related Container Introduction

The elements in the associative container are saved and accessed by keyword. The two main associative container types are map and set. The element in map is a keyword-value pair. Each element in the set contains only one keyword. The containers that allow duplicate keywords are multimap and multiset. To add Unorder_ in front of the disorder.

2. Using Associative containers

Use map://equivalent to a person's name-phone number

Map<string,size_t> Word_count;

string Word;

while (Cin>>word)

++word_count[word];// Extracts a word's counter and adds 1 to it.

Use set: equivalent to a person's name

3. Overview of associative containers

General operations for common containers are supported, but location-related operations, such as Push_back, are not supported.

(1) Multimap and multiset allow more than one element to have the same keyword.

(2) Requirements for keyword types

The keyword type of an ordered container

Use a comparison function of the keyword type:

Multiset<sales_data,decltype (COMPAREISBN) *> Bookstore (COMPAREISBN);//COMPAREISBN is a function that compares ISBN and returns a Boolean value.

When using Deltype to obtain a function pointer type, you must add a * to indicate that we want to use a pointer of the given function type. Indicates that when an element is added to bookstore, the elements are ordered by calling COMPAREISBN.

(3) Pair type

defined in the header file utility. A pair holds two (public) data members. Assuming that W is the pair defined, access two data members through W.first and W.second respectively.

To create a pair object's function:

Pair<string,int>

Process (vector<string> &v) {
if (!v.empty ())

Return{v.back (), V.back (). Size ()};

Else

return pair<string,int> ();}

(4) operation of the associated container

Key_type This is a keyword type

Mapped_type This is the type associated with the keyword

Value_type is the same for set, and Key_value. For map, for Pair<const key_value,mapped_type>

A. Associative container iterators

When you dereference an iterator for an associative container, we get a reference to the value of the container type Value_type.

Map Value_type is a pair, we can change the value of the pair, but we cannot change the value of the keyword. Set is the same, the keyword cannot be changed.

B. Associative containers and algorithms

We do not usually use generic algorithms for associative containers. Because the keyword is const, it means that it can only be read-only.

C. Adding elements

The insert member of the associated container adds or removes an element. Because the map and set contain non-repeating keywords, inserting an already existing keyword has no effect on the container.

Add elements to map: When you insert a map, you must remember that the element type is pair. So to create a pair in the Insert parameter list.

The insert return value depends on the container type and parameters.

For + + ((ret.first)->second), the understanding:

RET saves the value returned by insert, which is a pair

Ret.first is the first member of the pair and is a map iterator that points to the element with the given keyword

Ret.first-> This iterator to extract the elements in the map

D. Deleting an element

The correlation container defines three versions of erase, in addition to an iterator and an iterator pair. A erase that accepts the keyword type key_value parameter is also provided. Returns the number of deleted elements.

E. Subscript operation of Map

Map <string,size_t> word_count;//Empty map

word_count["ANNA"]=1;

Program execution steps: First look for Anna, not found. Then a new keyword, Anna, initializes the value to 0. Extracts the newly inserted element and assigns 1 to it.

The Mapped_type object is returned when the map is subscript, and a Value_type object is obtained when the map iterator is dereferenced.

Subscript and at Operation values for non-const map and Unordered_map

F. Accessing elements

Finds whether a particular element is already in the container, uses find, and uses count for duplicate keywords.

If you use subscript to find an element, if the keyword is not in the map, the following table operation inserts the keyword, causing side effects.

Lower_bound (k)//returns an iterator that points to the element with the first keyword not less than k

Lower_bound (k)//returns an iterator that points to an element with the first keyword greater than k

Equal_range (k)//Returns an iterator pair that represents the range of the keyword equal to K, and if K does not exist, the two members of the pair are equal to c.end ();

4. Unordered containers

Defines 4 unordered associative containers that do not use comparison operators to organize elements, but instead use the = = operator of a hash function and a keyword type.

Disorderly behavior is that the number of words in a sentence is not likely to be output in dictionary order.

Unordered containers are organized as a set of buckets on storage and are allowed for different keywords to be mapped to the same bucket. The functions for managing buckets are shown in p395.

unordered containers Use = = to compare elements, and to use an object of type hash<key_type> to generate a hash value for each element

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