Associated container surface

Source: Internet
Author: User

1. Three constructor functions for associating containers (C is the container type, T is the key value type of the number of containers, and C is the container name ):

C <t> C; // create an empty container

C <t> C1 (C2) // create a new container and copy all the elements of C2 to C1, that is, a copy of C1 as C2.

C <t> C (B, E) // copy the elements in the range of B and E to C. The elements in the range must be of the corresponding type.

2. operations supported by the associated container:

1. Begin, end, rbegin, rend

2. relational operation

3. Swap (), does not support assign ()

4. Clear (), erase ()

5. Associated containers do not support front, push_front, pop_front, back, push_back, and pop_back.

6. Add some unknown ones later.

Iii. Strictly weak sorting of key values

The key of the associated container not only has a type, but also has a related comparison function. Strictly ordered, that is, the number of adjacent elements can only be "<" or "= ".

Of course, this comparison function can be changed, but it is "<" by default. This ensures that the access is performed in ascending order when the container is traversed.

4. add elements to map

1. subscript Addition

Map <string, int> count_word;

String word;

While (CIN> word ){

++ Count_word [word];

}

Add With subscript. If the key value already exists, no operation is performed and the corresponding key value is returned. If the element does not exist, add a new element to the container and use the default function of the corresponding value to initialize the value. The above code will automatically initialize the value to 0 if the value type is int, this is the biggest feature of map.

2. Use Insert ()

Three operations (M is the container name, e is the pair type ):

M. insert (e) If e. if the first element does not exist in the container, add a new element to the container and initialize the corresponding value to E. second; if E. if first exists, no operation is performed. In both cases, a pair (iterator, bool) type is returned, and iterator is directed to E. first iterator. If bool is bool, true or false respectively indicate whether to insert e to the container.

M. insert (beg, end) beg, end is the iterator range, this insert executes the first operation for each element in this range, the element type referred to by the iterator must be consistent with the value_type of the container. The return type is void;

M. insert (ITER, e) First, the type of e must be consistent with value_type. Start with ITER, search, and execute the first operation. However, only one iterator is returned, the location of E in the container.

 

5. map deletion Elements

Three operations (M is the container name, k is the key, P, B, e is the iterator ):

M. Erase (k) // Delete the element whose key is K and return the number of deleted elements. Here, map only returns 0 or 1. If it is in multimap, it may not.

M. Erase (p) // Delete the element pointed to by P in the iterator, provided that it exists in m and the return type is void.

M. Erase (B, E) // Delete the elements in the range of B and E iterators. B and E must mark a valid range in m and the reverse type is void;

Operations in steps 6, 4, and 5 are also suitable for the set type, and are also suitable for the multimap and Multiset types, but there is a gap in the returned values of erase.

 

VII. Count and find operations

These two operations apply to all associated containers (M is the container name, k is the key ).

M. Count (k) // returns the number of occurrences of K, which may be 0 or 1 in map and set, but may be larger in multimap and Multiset;

M. Find (k) // The iterator that returns the first found key. Only exist and does not exist in map and set. There are multiple such keys in multimap and Multiset.

 

8. multimap and Multiset queries

1. Find and count

Typedef multimap <string, string >:: size_type sz_type;

Sz_type entries = authors. Count (search_item );

Multimap <string, string >:: iterator iter = authors. Find (search_intem );

For (sz_type I = 0; I <sztype; I ++ ){

Cout <ITER-> second <end;

ITER ++;

}

First, call count to find the number of key values to be queried, then call find to find the first position that appears, and then output one by one, because by default, containers are traversed in ascending order, therefore, you can find all the elements whose keys are search_item;

2. Use an iterator to solve the problem

First, let's look at two functions.

M. lower_bound (k) // returns an iterator pointing to the first element with no less than K;

M. upper_bound (k) // returns an iterator pointing to the element of the first key size K;

 

Typedef multimap <string, string >:: ierator authors_it;

Authors_it beg = authors. lower_bound (search_item );

End = authors. upper_bound (search_item );

Whlie (beg! = END ){

Cout <beg-> second <Endl;

Beg ++;

}

That is also the sentence. containers are traversed in ascending order;

3. performance_range ()

Eqal_range (k) // return a pair type. First and second are both iterators, first is equivalent to lower_bound, and second is equivalent to upper_bound, representing the range of iterator whose key value is K, that is, this function is equivalent to two functions in 2. The usage is also obvious.

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.