C + + Primer Learning Summary 11th Chapter related container

Source: Internet
Author: User

11th Chapter related Containers


1. A simple example of using map:

The result of the operation is:

2. Initialize the map and set:

3. The difference between set and Multiset:

4. When using map and set, the element type must be strictly weak (that is, the < comparison is defined ), and if the element type does not have a defined < number operation, it can be compared by an external function:

5. Pair type: Pair is a template that is used to generate a specific type. The default constructor for the pair initializes the value of the data member .

6. Traverse the map and set associated containers:

7. How to add the specified element to set and map:

Note: The return value of the insert Insert element in map is a pair object . The first member of the pair object is a pointer to the insertion element iterator, and the second member is a flag that inserts a successful bool value.

8. Add elements to the Multimap:

9. Remove the element from the map:

from Multimap Delete elements in the same way as the Map identical .

Map subscript operations, such as map["a"]:

If "A" is in the map, the operation returns the value corresponding to "a". Otherwise, the operation inserts the keyword "a" in the map and initializes the value.

11. Find the specified element in map or Multimap.

The following 5 member functions are available:

C.find (k): An iterator that returns KEY==K

C.count (k): Returns the number of elements in the Key==k

C.lower_bound (k): An iterator that returns the first element of a key>=k

C.upper_bound (k): An iterator that returns the first element of a key>k

C.equal_range (k): Returns the pair object of the left-closing right-open iterator for Key==k, which contains two iterators that point to the first pair object in the map that meets the requirements. The second iterator points to the next position of the last pair object in the map that meets the requirements.

C + + Primer Learning Summary 11th Chapter related container

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.