C + + STL container learning use (vector, queue, list, set, map)

Source: Internet
Author: User

 1.STL composition:

STL there are three core parts: Containers ( Container ), Algorithms ( Algorithms ), iterators ( Iterator ), the container adapter ( Containeradaptor ), the function object (functor) , in addition to other STL other standard components. In layman's words:

container: Something to fill, a cup to fill with water, a sea of salty water, a classroom for people ... STL container is a template class that can hold some data.

algorithm: is to pour water into the cup, to the big sea sewage, from the classroom to expel people ... STL is the method and operation of processing the data inside the container.

iterator: The water bottle poured into the cup, the sewer pipe, the property management personnel who kicked the man ... STL iterator: An object that iterates through the data in the container. When processing data stored in a container, iterators can move from one member to another. He can move among the members of certain containers in a predefined order. Iterators are pointers to normal one-dimensional arrays, vectors, double-ended queues, and lists.

containers: Learning list/vector/deque/set/multisets/map/multimaps/

( 1 ) Vector : Dynamic array, vector. (See Code for specific usage)

#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include < queue> #include <stack> #include <vector> #include <list> #include <set> #include <map> using namespace std; #define N 10010#define LL __int64typedef vector<int> intvector;typedef deque<int> Intdeque;int Main () {freopen ("in.in", "R", stdin);//freopen ("Out.out", "w", stdout); int n,m;vector <int> vec1;// VEC1 is initially empty vector <int> vec2 (10,6),//VEC2 originally consists of 10 elements with a value of 6 vector <int> VEC3 (Vec2.begin (), Vec2.begin () +3); VEC3 initially consists of 3 elements with a value of 6. Declaration iterator vector<int>:: Iterator i;//displays data from the VEC1 in the previous direction: iterator output using *icout<< "Vec1.begin ()---vec1.end ():" << Endl;for (I=vec1.begin (); I!=vec1.end (); i++) {cout<<*i<< "";}  cout<<endl;//Test Add and insert member functions (Note: Vectors do not support front insertion) Vec1.push_back (2); Add a member from behind Vec1.push_back (4); Vec1.insert (Vec1.begin () +1,5); Insert Member 5vec1.insert (Vec1.begin () +1,vec3.begin (), Vec3.end ()) in the first position of VEC1; Insert all members of VEC3//test assignment in the first position of VEC1The member function (re-assigns a value to the element on one of the vectors) vec2.assign (8,1);   The 8-member initial value of VEC2 is set to 1//to access elements in a position in the vector array cout<<vec2[2]<<endl;//Delete and remove vec1.pop_back (); Delete the last element of Vec1 Vec1.erase (Vec1.begin () +1,vec1.end ()-2);cout<< "Vec.pop_back () and Vec1.erase ():" <<  Endl;for (I=vec1.begin (); I!=vec1.end (); i++) cout<<*i<< ""; cout<<endl;//Display sequence message: The array in the vector is also from 0 to Vec1.size (), left closed right//get vector size: vec1.size ();//Empty vector:vec1.clear (); Vec1.clear ();cout<< "Vec1.empty ():" <<endl;for (I=vec1.begin (); I!=vec1.end (); i++) cout<<*i<  < ""; Cout<<endl;return 0;}


( 2 ) Queue : Dual-ended queue: Support Vector not supported by Push_front ();

void Put_deque (Intdeque deque,char *name) {intdeque::iterator pdeque;  Defines the iterator output cout<< "The contents of" <<name<< ":"; for (Pdeque = Deque.begin ();pd eque!=deque.end (); pdeque++) {cout<<*pdeque<< "";} Cout<<endl;} int main () {///test deque function Functions intdeque deq1;intdeque deq2 (10,6) Intdeque:: iterator i;//insert element at end and print deq1.push_back (2); Deq1.push_back (4);p Ut_deque (deq1, "deq1");//Add two elements in front of Deq1.push_front (5);d Eq1.push_front (7);p Ut_deque (deq1, "deq1" );//intermediate insertion of data Deq1.insert (Deq1.begin () +1,3,9);  Insert three 9put_deque (deq1, "deq1");//data access: both represent the following coordinates cout<<deq1[1]<< "" <<deq1.at (5) <<endl;// Data deletion: Remove//deq1.pop_front () from front and back, and by location,//deq1.pop_back ();d eq1.erase (Deq1.begin () +4); Delete the number below the coordinate of 4. Put_deque (deq1, "deq1");//Data modification deq1.assign (8,1);   All assignments for 1put_deque (deq1, "deq1");d eq1.clear ();  Empty Dequeput_deque (deq1, "deq1"); return 0;}


( 3 ) List : Linked list, doubly linked list, can only be accessed sequentially, cannot be used [] random access

void Printlist (list<int> N) {for (List<int>::iterator i=n.begin (); I!=n.end (); i++) cout<< *i << ""; Cout<<endl;} int main () {list<int> list1,list2;list1.push_back (123); List1.push_back (0); list1.push_back; list2.push_ Back (+); List2.push_back (12);//Test List sort function printlist (list1); List1.sort ();p rintlist (List1); List2.sort (); List1.merge (LIST2);  Two lists are sorted and merged, and are still ordered printlist (LIST1); return 0;}



( 4 ) Set and the multisets : Set and multi-set: The default is a good order!

· Set : contains element unique

· multisets : Contains elements that are not unique

int main () {set<int> set1;for (int i=0;i<10;i++) Set1.insert (i); for (Set<int>::iterator P = set1.begin (); P!=set1.end ();p + +) {cout<<*p<< "";} cout<<endl;//calculates the number of elements in the set that have a value of 2 Cout<<set1.count (2) <<endl;//insert element: repeatable multiset<int> A; A.insert (Set1.begin (), Set1.end ()); A.insert (4);  After insertion, the default is ordered for (Multiset<int>::iterator I=a.begin (); I!=a.end (); i++) cout<<*i<< ""; Cout<<endl;return 0;}


( 5 ) Map and the Multimaps : Mappings and multiple Mappings

Setin theKeyand thevalueis aKeytype of, andMapin theKeyand thevalueis apairtwo components in a structure. MapThe following table operators are supportedoperator[],Access by accessing a normal arrayMap, but the subscript isMapthe key. In theMultimapOne of the keys can correspond to several different values.

int main () {///defines Map:char is the type of the key, int is the type of the value map<char,int,less<char> > map1;map<char,int,less<char> >::iterator mapiter;//Note here: Two >> symbols to have a space//initialization map1[' c '] = 3;map1[' d '] = 4;map1[' a '] = 1;//print for (mapiter= Map1.begin (); Mapiter!=map1.end (); mapiter++) {cout<< "" << (*mapiter) .first<< ":" << (*mapiter ). Second;} cout<<endl;//where first corresponds to the char key in the definition, second int//in the corresponding definition retrieves map<char,int,less<char> >::const_iterator t = Map1.find (' d ');cout<< "T:" << (*t) .first<< "corresponding value:" << (*t) .second<<endl;cout<< map1[' d ']<<endl<<endl; You can also access it directly//-----------------------Multimap------------------------multimap<string,string,less<string > >mulmap;multimap<string,string,less<string> >::iterator p;typedef multimap<string,string, less<string> >::value_type vt;//Note the [] subscript operation in map is not supported in Multimap!!! Mulmap[string ("Songjs")] = string ("Bjfu"); ---is the wrong Mulmap.insert (VT (String ("Songjs"), String ("is a BOy ")), Mulmap.insert (VT (String (" Songjs "), String (" is a Girl ")),//Print output for (P=mulmap.begin ();p!=mulmap.end ();p + +) { cout<< (*p) .first<< "" << (*p). Second<<endl;} return 0;}

C + + STL container learning use (vector, queue, list, set, map)

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.