4, super standard library
What is there in the standard library, the maximum difference between the C standard library should be STL. With STL, you don't have to write most standard data structures and algorithms, and you can get very high performance.
There are several basic concepts in STL:
Container: The data structure of various data types can be accommodated.
Iterator: Access the structure of data in the container in turn
Algorithm: Function for some operations for containers via iterators
Emphasically understand:
An array is a container, and the pointer is an iterator.
Next, a few sections will be used to specifically describe the STL's profile.
The following will be taken from the C Standard Library "book, and will not be described below.
4.1 Containers in STL
There are 7 types of standard containers, and there is a corresponding expansion.
The model corresponding to the seven container is as follows:
Vector
The arrow indicates the direction of data growth. In fact, it is a dynamic array. It has better properties to delete elements at the end.
DEQUE
It has better performance to increase the elements at both ends.
List
Bidirectional linked lists have similar performance in any location.
The above three containers are referred to as sequence container. The insertion position of the element is independent of the value of the element.
SET / MULTITISET:
The elements in this container are sequentially, inserted into any element, which determines its position according to the corresponding sort criterion.
The same elements are not allowed in the SET, and there is the same element to be allowed in the MultiSet.
Map / MultiMap:
The Map is different from MultiMap in whether the same elements are allowed.
Map and SET differ in that MAP is stored in the pair of Key / Value.
And sort the elements according to Key.
The above four containers are referred to as an associated vessel (ASSOCIATIVE CONTAINER). Features have very good performance when looking up.
Based on the above 7 containers, STL also implements Stacks, Queues, Priority Queues.
Use MAP to give an example:
TypeDef Map
//definition
MapFoCcount Lunch;
// assignment
Lunch ["Chen Yong"] = 50.00;
Lunch ["Lin Lijian"] = 35.00;
Lunch ["陈阵"] = 45.00;
Lunch ["CZZS"] = 65.00;
MapForaccount :: item POS;
For (POS = Lunch.Begin (); POS! = Lunch.end (); POS)
{
Cout << "Name: << Pos-> first <<" / t "
<< "balance:" << POS-> Second << ENDL;
}
All of the above various containers have some member functions, support some basic operations and optimize certain algorithms.
The members of the different containers are not exactly the same.
among them:
l All containers support the following operators: ==,! =, <,>, <=,> =. When and only the two container types are the same, the number of elements is the same, the element order is the same, and each element is equal, == True.
The case is determined in a word ordered.
L insert (), push_front (), push_back (), add members to add a function
l Remove (), POP_FRONT (), POP_BACK (), [], AT (), etc. to delete and read elements.
In short, these functions make you read or write to the elements in the container.
The remaining member functions, such as sort (), merge (), etc., the container is optimized according to the characteristics of its own characteristics, and achieve the same function as the algorithm described below.
Can be used as a very common use of a dynamic array, and take a look at the container for example. What can we do.
Construction and sect
Vector
Vector
Vector
Vector
Vector
c. ~ vector
Non-variable operation
C.Size () Returns the number of elements in the container
C.empty () size is 0
C.max_size () can accommodate the maximum number of elements
Capacity () The maximum number of elements that can be accommodated before space
Reserve () keeps a certain size space
C1 == C2
C1! = C2
C1 C1> C2 C1 <= C2 C1> = C2 Assignment operation C1 = C2 copies all Elements of C2 to C1 C.ssign (N, ELEM) Pack C with n elements C.ssign (beg, end) Fill C with the contents of the specified interval C1.SWAP (C2) C1, C2 content interchange SWAP (C1, C2) is the same as global function Element access C.at [IDX] Returns the element represented by the index IDX, check the boundary C [IDX], but not check the boundary C.front () Returns the first element and does not check if it exists C.back () Returns the last element and does not check if it exists Iterator related functions c.begin () Returns random access iterators to the first element c.End () Returns a random access iterator to the last element C. RBEGIN () Returns the reverse iterator pointing to the first element C.Rend () Returns the reverse iterator pointing to the last element Plug and removal operation C.insert (POS, ELEM) inserts a new element copy in the POS location and returns the location of the new element C.insert (POS, N, ELEM) Insert N new element copy in POS location C.insert (POS, BEG, END) copy of all elements in the POS position inserted [BEG, END) interval C.PUSH_BACK (ELEM) Add a ELEM at the end C.POP_BACK () Remove the last element, do not return C.RASE (POS) Remove the element of the POS location, return to the next element C.RASE (BEG, END) Removes all the elements in the [BEG, END) interval, and back to the new location C.Resize (NUM) change the number of elements to NUM C. CLEAR () Removes all elements to see the following block: This is done to illustrate the use of various member functions of the Vector. TypedEf Vector // Print_Elements is responsible for outputting all the elements in the container Template Inline Void Print_Elements (Const T & Coll, Const Char * CPTCSTR = ") { TypeName T :: const_iterator pos; COUT << CPTCSTR << Endl; For (POS = Coll.begin (); POS! = COLL.End (); POS) { Cout << * pOS << ''; Cout << Endl; } } int main () { StringArray filename; // EMPTY VECTOR / / Do not reassign the memory before the number of elements in the container reaches 10 Filename.Rserve (10); FILENAME.PUSH_BACK ("C: //Test1.emf"); Filename.push_back ("c: //test2.emf"); Filename.push_back ("c: //test3.emf"); Filename.push_back ("c: //test4.emf"); Filename.push_back ("c: //test5.emf"); Filename.push_back ("c: //test6.emf"); Print_Elements (FileName, "After Push_Back:"); Cout << "max_size ():" << filename.max_size () << endl; COUT << "size ():" << filename.size () << endl; Cout << "Capacity (): << FileName.capacity () << endl; // Insert in the first element Filename.Insert (filename.begin (), "d: //ie.emf"); Print_Elements (FILENAME, "After Insert:"); Cout << "now the first element is:" << filename [0] << endl; // back () Returns the last element Cout << "Now the last element is:" << filename.back () << endl; / / Getting initial value for TempFileName StringArray TempFileName = filename; Print_Elements (TempFileName, "Before ERASE:"); // Delete the top three elements TempFileName.rase (TempFileName.Begin (), TempFileName.begin () 3); Print_Elements (TempFileName, "After Erase:"); / / Delete the last element TempFileName.POP_BACK (); Print_Elements (TempFileName, "After Pop_back ():"); // Take a horror, this note vector is continuous in memory Vector v.resize (41); STRCPY (& V [0], "This Is A Test"); Printf ("% s / n", & v [0]); Return 0; } The output is: After Push_Back: C: /TEST1.EMF C: / Test2.emf C: / Test3.emf C: /TEST4.EMF C: / Test5.emf C: / Test6.emf MAX_SIZE (): 357913941 Size (): 6 Capacity (): 10 After Insert: D: /ie.emf C: /TEST1.EMF C: / Test2.emf C: / Test3.emf C: /TEST4.EMF C: / Test5.emf C: / Test6.emf Now the first element is: d: /ie.emf Now the last element is: c: /test6.emf Before ERASE: D: /ie.emf C: /TEST1.EMF C: / Test2.emf C: / Test3.emf C: /TEST4.EMF C: / Test5.emf C: / Test6.emf After Erase: C: / Test3.emf C: /TEST4.EMF C: / Test5.emf C: / Test6.emf After pop_back (): C: / Test3.emf C: /TEST4.EMF C: / Test5.emf this is a test