C ++ STL (vector, map, set, list) member function arrangement, stlvector

Source: Internet
Author: User

C ++ STL (vector, map, set, list) member function arrangement, stlvector

/* In the recent ACM competition, I forgot the member function when I used it. The thief was embarrassed and made preparations for the future competition */
LIST: constructor list <int> c0; // empty linked list <int> c1 (3 ); // create a list containing three elements whose default value is 0 <int> c2 (); // create a list containing five elements, the values are both 2 lists <int> c4 (c2); // create a copy linked list of c2 <int> c5 (c1.begin (), c1.end ()); /// c5 contains the element [_ First, _ Last) of c1 in a region ). The member function c. begin () returns the iterator pointing to the first element of the linked list. C. end () returns the iterator that points to the last element of the linked list. C. rbegin () returns the first element of the reverse linked list, that is, the last data of the c linked list. C. rend () returns the next position of the last element of the reverse linked list, that is, the first position of the first data in the c linked list. Operator = overload value assignment operator. C. assign (n, num) Copies n num and assigns it to linked list c. C. assign (beg, end) copies the elements in the [beg, end) interval and assigns them to the linked list c. C. back () returns the last element of linked list c. C. front () returns the first element of linked list c. C. empty () checks whether the linked list is empty. C. size () returns the number of actual elements in linked list c. C. max_size () returns the maximum number of elements that a linked list c may hold. C. clear () clears all elements in linked list c. C. insert (pos, num) inserts the element num at the pos position. C. insert (pos, n, num) inserts n element num at the pos position. C. insert (pos, beg, end) inserts an element with a range of [beg, end) at the pos position. C. erase (pos) deletes the elements at the pos position. C. push_back (num) adds an element to the end. C. pop_back () deletes the end element. C. push_front (num) adds an element at the beginning. C. pop_front () deletes the first element. Resize (n) is used to specify the length of a new linked list. If the length is greater than the original length, 0 is used to replace it. Resize (n, num) is used to define the length of a new linked list. The part beyond the original length is replaced by num. C1.swap (c2); Switch c1 and c2. Swap (c1, c2); same as above. C1.merge (c2) merges two ordered linked lists and puts them in the order of c1 and releases c2. C1.merge (c2, comp) merges two ordered linked lists and sorts them according to the custom rules, and then places them in c1 to release c2. C1.splice (c1.beg, c2) connects c2 to the beg position of c1 and releases c2c1. splice (c1.beg, c2, c2.beg) connects the beg element of c2 to the beg position of c1, and removes the element c1.splice (c1.beg, c2, c2.beg, c2.end) in c2) connect the [beg, end) element of c2 to the beg position of c1 and release the [beg, end) element of c2 to remove (num) the element matching num in the linked list. Remove_if (comp) deletes elements that meet the conditions. The parameter is a custom callback function. Reverse () reverse linked list unique () delete adjacent elements c. sort () sorts linked lists. By default, the list is sorted in ascending order c. sort (comp) custom callback function to implement custom sorting overload operator = operator! = Operator <= operator> = set: begin (), return the first end () element of the set container, return the last clear () element of the set container (), delete all the elements in the set container empty () and determine whether the set container is empty max_size (). The maximum number of elements that the set container may contain is returned. size (), returns the number of elements in the current set container rbegin. The returned value is the same as end (), and the returned value is the same as rbegin () used to find the number of times a key value appears in the set. This function is not very practical in the set, because a key value may only appear 0 or 1 time in the set, so it changes to determine whether a key value has appeared in the set. Example: cout <"the number of occurrences of 1 in set is:" <s. count (1) <endl; erase (iterator), delete the erase (first, second) value pointed to by the iterator, delete the erase (key_value) value between the first and second locators ), delete the value of the key value key_value find () and return the value to the value locator. If the value is not found, return end (). Insert (key_value); key_value is inserted into the set, and the returned value is pair <set <int >:: iterator, bool>. bool indicates whether the insert is successful, while iterator indicates the position of the insert, if the key_value is already in set, the key_value indicated by iterator is located in set. Inset (first, second); inserts the elements between the first and second positions into the set. The returned value is void. lower_bound (key_value), returns the first locator upper_bound (key_value) greater than or equal to key_value, returns the last locator vector: at () greater than or equal to key_value, returns the element back () at the specified position () returns the last element end (). returns the iterator of the last element. capacity () returns the number of elements that a vector can hold (without re-allocating memory) clear () clears all elements empty () to determine whether the Vector is null (null when true is returned) erase () deletes the specified Element c. erase (beg, end) front () returns the first element insert () inserted into the Vector max_size () returns the Vector Maximum number of elements that can be accommodated (upper limit) pop_back () remove the last element push_back () add an element rbegin () at the end of the Vector and return the reverse iterator rend () at the end of the Vector () returns the inverse iterator starting with the Vector. reserve () sets the smallest element capacity of the Vector. resize () changes the size of the number of Vector elements. size () returns the size of the number of Vector elements. swap () exchange two VectorMap: the map function automatically creates a Key-value correspondence. Key and value can be any type you need. You can quickly search for records based on the key value. The search complexity is basically Log (N). If there are 1000 records, you can search for up to 10 records, 1,000,000 records, and up to 20 records. Insert Key-Value records quickly. Quickly delete a record and modify the value record according to the Key. Traverse all records. Member variables and member functions 1. map: the most basic constructor; map <string, int> mapstring; map <int, string> mapint; map <sring, char> mapstring; map <char, string> mapchar; map <char, int> mapchar; map <int, char> mapint; 2. map to add data; map <int, string> maplive; 1. maplive. insert (pair <int, string> (102, "aclive"); 2. maplive. insert (map <int, string >:: value_type (321, "hai"); 3, maplive [112] = "April "; // Add the simplest and most common insert operations in map! 3. Search for elements in map: The find () function returns an iterator pointing to an element whose key value is key. If no key is found, the iterator pointing to the end of map is returned. Map <int, string >:: iterator l_it; l_it = maplive. find (112); if (l_it = maplive. end () cout <"we do not find 112" <endl; else cout <"wo find 112" <endl; 4. Delete elements in map: if you delete the 112; map <int, string >:: iterator l_it; l_it = maplive. find (112); if (l_it = maplive. end () cout <"we do not find 112" <endl; else maplive. erase (l_it); // delete 112; 5. swap usage in map: swap in Map is not an element exchange in a container, but an exchange between two containers; int main () {map <int, in T> m1, m2, m3; map <int, int >:: iterator m1_Iter; m1.insert (pair <int, int> (1, 10); m1.insert (pair <int, int> (2, 20); m1.insert (pair <int, int> (3, 30); m2.insert (pair <int, int> (10,100 )); m2.insert (pair <int, int> (20,200); m3.insert (pair <int, int> (30,300); cout <"The original map m1 is :"; for (m1.iter = m1.begin (); m1_Iter! = M1.end (); m1_Iter ++) cout <"" <m1_Iter-> second; cout <". "<endl; // This is the member function version of swap // m2 is said to be the argument map; m1 the target map m1.swap (m2 ); cout <"After swapping with m2, map m1 is:"; for (m1_Iter = m1.begin (); m1_Iter! = M1.end (); m1_Iter ++) cout <"" <m1_Iter-> second; cout <". "<endl; cout <" After swapping with m2, map m2 is: "; for (m1_Iter = m2.begin (); m1_Iter! = M2.end (); m1_Iter ++) cout <"" <m1_Iter-> second; cout <". "<endl; // This is the specialized template version of swap (m1, m3); cout <" After swapping with m3, map m1 is :"; for (m1.iter = m1.begin (); m1_Iter! = M1.end (); m1_Iter ++) cout <"" <m1_Iter-> second; cout <". "<endl;} 6. map's sort problem: the elements in Map are automatically sorted by key in ascending order, so the sort function cannot be used for map: 7. Basic map operation function: C ++ Maps is an associated container that contains "key words/Values" for begin () to return the iterator pointing to the map header clear () to delete all elements count () returns the number of times a specified element appears. empty () returns true end () If map is empty. returns the iterator erase () pointing to the end of the map to delete an element find () find an element insert () insert the element key_comp () and return the lower_bound () Return key value> = the first position of the given element max_size () returns the maximum number of elements that can be accommodated. rbegin () returns a reverse iterator rend () pointing to the end of the map. Returns a reverse iterator size () pointing to the map header () returns the number of elements in the map. swap () exchanges two map upper_bound () Return key values.> value_comp () returns the function of comparing element values.

  

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.