C++stl (vector,map,set,list) member function collation

Source: Internet
Author: User
Tags sorts

/* recently ACM game, used to forget the member function, the thief embarrassed, to the next race to do the preparation * *
LIST: constructor list<int> C0; Empty list list<int> C1 (3); Build a list of elements with three default values that are 0 list<int> C2 (5,2); Build a list of five elements with a value of 2 list<int> c4 (C2); Build a C2 copy linked list list<int> c5 (c1.begin (), C1.end ()); C5 contains c1 elements of an area [_first, _last]. The member function C.begin () returns an iterator that points to the first element of the list. C.end () returns an iterator that points to the last element of the list. C.rbegin () returns the first element of the reverse list, the last data of the C-linked list. C.rend () returns the next position of the last element of the reverse list, that is, the first data of the C-linked list, and then the forward position. Operator= the overloaded assignment operator. C.assign (n,num) assigns n num copies to the list C. C.assign (beg,end) assigns the element copy of the [Beg,end] interval to the list C. C.back () returns the last element of the linked list C. C.front () returns the first element of the chain list C. C.empty () determines whether the linked list is empty. C.size () returns the number of actual elements in the chain list C. C.max_size () returns the maximum number of elements that the list C may hold. C.clear () Clears all elements in the list C. C.insert (Pos,num) inserts the element num at the POS location. C.insert (Pos,n,num) inserts n element num at the POS location. C.insert (pos,beg,end) inserts an element at the POS location that has an interval of [beg,end]. C.erase (POS) deletes the pos location element. C.push_back (num) Adds an element at the end. C.pop_back () deletes the element at the end. C.push_front (num) Adds an element at the beginning of the position. C.pop_front () deletes the first element. Resize (n) A new definition of the length of a linked listThe portion of the original length is replaced with 0, which is less than the original part removed. Resize (n,num) from the new definition of the length of the list, beyond the original length of the section with num instead.            C1.swap (C2); Swap the C1 and C2.            Swap (C1,C2); Ditto. C1.merge (C2) merges 2 ordered lists and puts them in order, releasing C2 from the new C1. C1.merge (C2,comp) merges 2 ordered linked lists and then sorts them into C1 after sorting by custom rules, releasing C2. C1.splice (C1.BEG,C2) connects C2 to the beg location of C1, releasing C2c1.splice (C1.beg,c2,c2.beg) to connect the C2 location element to beg C1 position, and cast the beg position in C2 Element C1.splice (C1.beg,c2,c2.beg,c2.end) connects elements of C2 's [beg,end] position to C1 beg position and frees the element of C2 's [Beg,end] location from remove (num) to delete the element that matches num in the linked list. REMOVE_IF (comp) removes the element that the condition satisfies, and the parameter is a custom callback function. Reverse () Reverses the linked list unique () removes the adjacent element C.sort () sorts the linked list, the default ascending c.sort (comp) Custom callback function implements the custom sort overload operator Operator==oper Ator!=operator<operator<=operator>operator>=set:begin () returns the first element of the Set container end (), which returns the last element of the set container, clear (), delete all the elements in the Set container empty (), determine if the set container is empty max_size (), return the maximum number of elements that the set container may contain, size (), and return the number of elements in the current set container Rbe Gin, the returned value and end () are the same rend (), and the returned value is the same as Rbegin () count () to find the number of occurrences of one of the key values in the set.        This function in set does not          is useful, because a key value can only occur 0 or 1 times in set, so it changes Determines whether a key value has occurred in set. Example:cout<< "Set 1 occurrences are:" <<s.count (1) <<endl;erase (iterator), remove the locator iterator point to the value erase (first, Second), remove the value between the locator first and second erase (key_value), delete the value of the key value Key_value find (), return the given value of the locator, and return end () if not found. Insert (Key_value); Key_value inserted into set, the return value is Pair<set<int>::iterator,bool>,bool indicates whether the insertion was successful, and iterator represents the location of the insertion, if Key_ Value is already in set, then iterator represents the position of the key_value in set. Inset (First,second); Insert the element between the locator first and second into the set, the return value is Void.lower_bound (Key_value), and the locator Upper_bound (Key_value) that is greater than or equal to Key_value is returned. Returns the last key_value of a locator with a value greater than or equal to Vector:at () returns the element at the specified position back () returns an iterator to the end () that returns the final element (the next position of the end Element) capacity () Returns the number of elements the vector can hold (without reallocating memory) clear () Clears all elements empty () determines whether the vector is empty (null when returning True) erase () deletes the specified element c.erase (beg,end) front () Returns the first element insert () inserts an element into the vector max_size () returns the maximum number of elements the vector can hold (upper limit) pop_back () removes the last element push_back () adds an element at the end of the vector rbegin ()Returns the inverse iterator of the tail of the vector rend () returns the inverse iterator of the vector start reserve () sets the minimum element capacity of the vector resize () changes the size of the vector element number size size () returns the number of vector elements of swap () Exchange two VECTORMAP:MAP functions to automatically establish the corresponding key-value. The key and value can be any type you want. Quickly find records based on key values, finding the complexity is basically log (N), if there are 1000 records, find up to 10 times, 1,000,000 records, up to 20 times to find. Quickly insert Key-value Records. The quick Delete record modifies the value record according to key. Traverse all records. member variables and member functions 1.         Map's 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 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";//map the simplest and most common inserts to add!           The find of elements in 3,map: the Find () function returns an iterator that points to the element with key value, and returns an iterator to the tail of the map if it is not found.    Map<int, string >::iterator l_it;;   L_it=maplive.find (112);   if (L_it==maplive.end ()) cout<< "We do not find" <<endl; else Cout<< "Wo find" <<endl;4,map the deletion of elements: If you delete the Map<int, string >::iterator l_it;;   L_it=maplive.find (112);   if (L_it==maplive.end ()) cout<< "We do not find" <<endl;  else Maplive.erase (L_IT);      Use of swap in delete 112;5,map: Swap in map is not an element exchange in a container, but a two container exchange; int main () {map <int, int> 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 are 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 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 sort problem: The elements in map are automatically sorted by key in ascending order, so you cannot use the sort function for map: 7, the basic operation function of Map: C + + maps is an associative container that contains "keyword/value" to begin ()    Iterator to the map header clear () removes all elements count () returns the number of occurrences of the specified element empty () returns True if Map is empty ()       Returns an iterator pointing to the end of the map erase () deletes an element find () finds an element insert () inserts an element Key_comp () returns The function of the comparison element key Lower_bound () returns the 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 iteration to the tail of the map Rend () returns a reverse iterator to the map header size () returns the number of elements in the map swap () swap two map Upper_bou nd () returns the key value > the first position of a given element Value_comp () returns a function that compares the element value

  

C++stl (vector,map,set,list) member function collation

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.