C + + Learning basics four--sequential containers and associative containers

Source: Internet
Author: User

-Sequential containers: vector,list,queue
1. Common uses of sequential containers:

#include <vector> #include <list> #include <queue>

(1) Vector Declaration

Vector<string> Svec;
(2) Add element:

C.push_back (t): Adds an element with a value of t at the tail of container C. return void type
C.push_front (t): Adds an element with a value of t at the front end of container c. The return void type is only available for list and deque container types.
C.insert (p,t): Inserts a new element with a value of t before the element that the iterator p points to. Returns an iterator that points to the newly added element
C.insert (p,n,t): Inserts a new element with n values of T before the element that the iterator p points to. return void type
C.insert (p,b,e): Inserts an element in the range labeled by Iterators B and E before the element that the iterator p points to. return void type

(3) Access elements:

C.back (): Returns a reference to the last element of container C. If C is empty, the operation defines
C.front (): Returns a reference to the first element of container C. If C is empty, the operation defines
C[n]: Returns a reference to the element labeled N. If n <0 or n >= c.size (), the operation applies only to vector and deque containers
c.at (n) returns a reference to the element labeled N. If the subscript is out of bounds, the operation applies only to vectors and deque containers

(4) Delete element:

C.erase (p) Removes the element that the iterator p points to. Returns an iterator that points to the element following the deleted element. If P points to the last element within the container, the returned iterator points to the next position of the container's outside end. If the P-body is an iterator that points to the next position on the outside end.
C.erase (b,e): Removes all elements in the range marked by Iterators B and E. Returns an iterator that points to the element following the deleted element segment. If the E body is an iterator to the next position on the outside end, the returned iterator also points to the next position of the container's out end
C.clear (): Removes all elements in container C. return void
C.pop_back (): Removes the last element of container C. returns void.
C.pop_front (): Removes the first element of container C. returns void. If C is an empty container, the function applies only to the list or deque container

2. Advantages and disadvantages of sequential containers:

(1) Vector: Efficient random access, slow insertion or deletion of elements.
(2) List: The element can be inserted or deleted efficiently in any position.
(3) Queue: Efficient random access, effective insertion and deletion of elements from both ends, inserting elements at the beginning or end without invalidating any iterators, and deleting elements at the header or tail invalidates the iterator that points to the deleted element. Inserting or deleting an element at any other location in the queue invalidates the iterator that points to all elements of that container.

Second, the related container: Map,set
Common uses of 1.map containers:
Map-defined type: stored as key-value pairs

Map<k,v>::key_type: The type of the key used as the index in the map container
Map<k,v>::mapped_type: The type of the value associated with the key in the map container
Map<k,v>::value_type: A pair type whose first element has a const map<k, V>::key_type type, and second element is map<k, V>::mapp Ed_type type


(1) statement

Map<string,string> MP1;

(2) Insert element:
Subscript Method:

mp1["Tom"] = "DSJFKJF";

* * Note: When using the subscript method, if the map does not exist, insert the key directly and add the default type value.

Insert:mp1.insert (Map<string,string>::value_type ("Joeey", "DSJF"));

(3) Access elements:
Subscript Method:

string value1 = mp1["Tom"];

* * Note: When an element is accessed using the Subscript method, if the key is not in the map container, the subscript operation inserts a new element with that key.
Use the Count function:

Mp1.count ("Tom"); Returns the number of occurrences of "Tom"

Use the Find function:

Mp1.find ("Tom"); if there is an element in the map container indexed by "Tom", the iterator that points to that element is returned. If it does not exist, the end iterator is returned.

(4) Iterate through the elements:

Get iterator positioned on the first elementmap<string, int>::const_iteratormap_it = Word_count.begin ();//For EA CH element in the Mapwhile (map_it! = Word_count.end ()) {//Print the element key, value pairs cout << map_it->  First << "occurs" << map_it->second << "Times" << Endl; ++map_it; Increment iterator to denote the next element}

(5) Delete element:

M.erase (k): Removes the element with the M medium key as K. Returns a value of type Size_type that represents the number of deleted elements
M.erase (P): Removes the element that the iterator p points to from M. P must point to an element that does exist in M and cannot be equal to M.end (). return void
M.erase (b,e): Removes a range of elements from M, which is marked by iterators on B and E. B and E must mark a valid range in M: that is, B and E must point to the element in M or the next position of the last element. Also, B and e are either equal (the scope of the deletion is empty at this time), or the element that B points to must appear before the element that E points to. return void type

2.set: Only keys can be stored

Each key of a set container can only correspond to one element, and no subscript operator is provided.
Just as the key portion of an element in a map cannot be modified, the key in the set is also const.
(1) Adding elements to set: Insert operation is available.

Set<string> Set1;set1.insert ("the");

(2) Get elements from set: You can use the find operation to get an element from a set by key. If you simply determine whether an element exists, you can also use the count operation to return the number of elements in the set that corresponds to that key. Of course, for a set container, the return value of Count can only be 1 (the element exists) or 0 (the element does not exist).

C + + Learning basics four--sequential containers and associative containers

Related Article

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.