STL's associated container

Source: Internet
Author: User

Associative containers support efficient keyword lookups and access. The two main association container (associative-container) types are map and set. The standard library provides 8 associative containers, each of which is represented by a three dimension:

    • Or a set, or a map
    • or require a non-repeating keyword, or allow duplicate keywords
    • Save the element sequentially, or save it in an unordered order.

The first name of the container that allows duplicate keywords contains the word multi; the name of the container that does not keep the keyword stored sequentially begins with the word unordered.

The type map and definition in the multimap header file map , and the definition in the set multiset header file set, the unordered container is defined 在unordered_map and unordered_set in.

Associative container Overview Defining association Containers

Each association container defines a default constructor that creates an empty container of the specified type. You can also initialize an associative container to a copy of another container of the same type, or initialize the associative container from a range of values, as long as the values can be converted to the type required by the container. Under the new standard, you can also initialize the value of the associated container.

 Map<string, size_t>Word_count;//Empty container Set<string>Exclude = {"the","but","and","or"};//List initialization///Three elements: authors map last name to first name Map<string, string>Authors = {{"Joyce","James"},    {"Austen","Jane"},    {"Dickens","Charles"}};

When initializing a map, you must provide the keyword and value type. We enclose each keyword-value pair in curly braces: {key ,vaLue} To point out that together they form an element in the map.

Initializing Multimap and Multiset

The keywords in a map or set must be unique, and multimap and multiset do not have this limitation.

 vector<int>Ivec for( vector<int>:: Size_type i =0; I! =Ten;    i++) {ivec.push_back (i); Ivec.push_back (i);} Set<int>Iset (Ivec.begin (), Ivec.end ()); multiset<int>Miset (Ivec.begin (), Ivec.end ());cout<<ivec.size () <<endl;//Print outcout<<iset.size () <<endl;//Print out tencout<<miset.size () <<endl;//Print out
Pair type

defined in the header file utility.
A pair holds two numbers that are allegedly willing. Like a container, pair is a template that is used to generate a specific type. When creating a pair, we must provide two type names, and the data members of the pair will have the corresponding type. Two types do not require the same.

pair<stringstring> anon;pair<string, size_t> word_count;pair<stringvector<int>> line;

The default constructor of the pair initializes the value of the function member (string is initialized to an empty string and size_t is initialized to 0). You can also provide an initializer for each member:

pair<stringstring> author("James""Joyce");pair<stringstring> author()

Unlike other standard library types, the data members of the pair are public, and two members are named First and second, and we access them using normal member access symbols.

Other operations:

pair<string,string>Authorpair<string,string>Author1 ("AAAA","dddd");pair<string,string>author2{"AAAA","dddd"};pair<string,string>Author3={"AAAA","dddd"};//Returns a type of Pair,pair initialized with V1 and V2 inferred from the type of V1 and v2Auto P=Make_pair ( One, A); auto Q=Make_pair ("AAAA","BBBB");
Associative container operations
type aliases meaning
Key_type The type of the keyword for this container type
Mapped_type The type associated with each keyword; only applicable to map
Value_type For set, same as Key_type
For map, forpari<const key_type, mapped_type>

Since we cannot change the keyword of an element, the keyword portion of these pair is const. Only the map type defines the Mapped_type.

The value_type of a map is a pair, and we can change the value of the pair, but we can't change the value of the keyword member.

adding elements
c.insert(v) //v是value_type类型的对象c.emplace(args)c.insert(b,e) //b,e是迭代器c.insert(il) //il是value_type类型值的花括号列表c.insert(p, v) //类似于`insert(v)`,但将迭代器p作为一个提示,指出从哪里开始搜索新元素应该存储的位置c.emplace(p, args)
Delete Element
c.erase(k) //从c中删除每个关键字为k的元素,返回一个size_t类型的值,指出删除的元素的数量c.erase(p) //从c中删除迭代器p指定的元素,返回一个指向p之后元素的迭代器c.erase(b, e) 删除迭代器对b和e所表示的范围中的元素。返回e。
Subscript operation of Map

Similar to the other subscript operators, the map subscript operator takes an index that gets the value associated with this keyword. Unlike other subscript operations, if the keyword does not exist in the map, an element is created for it and inserted into the map, and the associated value is initialized with the value.

Operation meaning
C[K] Returns the element with the keyword K and, if it does not exist, adds an element with the keyword K, which is the value class.
C.at (k) Access the element with the keyword K, with parameter checking; if k is not in C, throw an out_of_range exception

The subscript operator of map returns an lvalue that can be read or written.

accessing elements
Operation meaning
C.find (k) Returns an iterator that points to the first element with the keyword K, and returns a trailing iterator if K is not in the container
C.count (k) Returns the number of elements that have the keyword equal to K. For containers that do not allow duplicates, the return value is always 0 or 1
C.lower_bound (k) Returns an iterator that points to an element that is not less than K for the first keyword
C.upper_bound (k) Returns an iterator that points to an element with the first keyword greater than k
C.equal_range (k) Returns an iterator pair that represents the range of elements that are equal to K for the keyword. If K does not exist, the two members of the pair are equal to C.end ()
Associative containers

The new standard defines 4 unordered associative containers (unordered associative container). Instead of using comparison operators to organize elements, these containers use a hash function and the = = operator of the keyword type.

In addition to hash management operations, unordered containers provide the same operations as ordered containers.

An iterator to an ordered container accesses the elements in the container in an orderly manner through the keyword. Elements with the same keyword are stored adjacent, whether in an ordered container or in an unordered container.

A hash function, a function that maps a value of a given type to an integer (size_t) value. Equal values must be mapped to the same integer, and unequal values should be mapped to different integers as much as possible.

Strict weak order: the relationship between the keywords used by the associated container. In a strict weak order, you can compare any of the two values and determine which is smaller. If any one is not less than the other, the two values are considered equal.

STL's associated 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.