STL Source First Contact
STL = Standard Template Library, the literal translation comes from: Standards templates, is the HP Lab developed a series of software collectively. Fundamentally, STL is a collection of "containers" that have list,vector,set,map and so on, and STL is also a collection of algorithms and other components. The collection of "containers" and algorithms here refers to many of the world's most intelligent people for many years. The purpose of STL is to standardize the components so that they do not need to be re-developed and can use ready-made components. STL is now part of C + +, so there is no additional installation. What STL implements is a conceptual structure built on the basis of generic thinking. Said so much still do not know what STL is a thing, today is just the first contact this concept, feel very advanced appearance, first such understanding, STL is a warehouse, a storage of various tools warehouse. Its tools are divided into six major categories (six components):
Container (containers): Various data structures, such as vector,list,deque,set,map, are used to store the underlying data. Generally there is a sequence (the following is the vector to write this is this), correlation and so on.
Algorithm (algorithms): A variety of commonly used algorithms such as: Sort,search,copy,erase ...
Iterator (iterator): plays the glue between the container and the algorithm, is called "Generic pointer", a total of 5 types, as well as their derivative changes. All STL containers come with their own dedicated iterators. The native pointer is also an iterator.
Functor (functor): A behavior-like function can be used as a strategy for an algorithm. General function pointers can be thought of as chivalrous imitation functions.
Adapter (Adapter): A thing used to modify a container, or an imitation function, or an iterator interface.
Configurator (allocators) is responsible for space configuration and management. Configurator is a class template that implements dynamic space configuration, space management, and space release.
Because the following is basically the simple operation of vector implementation, so just a little more about it. Vector is a dynamic space, with the addition of elements, its internal mechanism will expand its own space to accommodate the new elements. Therefore, the use of vectors for the rational use of memory and the use of flexibility has a great help. Vector maintenance is a continuous space, regardless of the type of element, the normal pointer can be used as a vector iterator to meet all necessary conditions.
Simple implementation of vectors:
1 #pragma once 2 #include <iostream> 3 #include <assert.h> 4 #include <stdlib.h> 5 using namespaceStd 6 Template<class t> 7 classVector 8{9 Public: Ten typedef t*Iterator; each typedef const T*Citerator; Public: 13Vector () 14: Start (NULL) 15, Finish (NULL) 16, Endofstorage (NULL) 17{} Vector (const vector<t>&V)//:start (new T[v.endofstorage-v.start])//, Finish (v.finish)//, Endofstorage (v.endofstorage) 22:start (new T[v.endofstorage-V.start]), Finish (Start + (V.finish-V.start)), Endofstorage (Start + (V.endofstorage-V.start)) {my_memcopy (start, V.start, sizeof (T) * (V.endofstorage-V.start)); 26} 27//deposit a size element into the vector 28Vector (citerator array, size_t size) 29:start (newT[size]) 30, Finish (start), endofstorage (start +Size) {size_t index = 0; index < size; + +Size) {Start[index] =Array[index]; 34} 35} vector<t>&operator= (const vector<t>&V) {PNS if (this! = &V) {vector<int>TMP (v); 39Swap (TMP); 40} return *this; 42} 43 ~Vector () {if(start) {Delete[] start; * start =NULL; +//delete[] finish; The finish =NULL; //delete[] endofstorage; Endofstorage =NULL; 51} 52} 53//Return iterator for first element 54Iterator Begin () {returnStart 56} citerator Begin () const{+/-returnStart 59} 60//Gets the next position of the last element in the vector 61Iterator End () {returnfinish:63} Iterator End () const{$ returnFinish 66} size_t size () const{Return Finish-Start 69} size_t Capacity () const{Endofstorage return-Start 72} (){return finish = =Start 75} t& operator[] (size_t index) {returnStart[index]; 78} t& Const operator[] (size_t index) const{returnStart[index]; 81} t&At (size_t index) {if (Index <= size ()) && (index >= 0))-ReturnStart[index]; 85} t& const at (size_t index) const{if (index <= Size ()) && (index >= 0)), returnStart[index]; 89} 90//Get the first element in the vector. t&Front () {srart[0 return]; 93} 94 const t& Front () const{start[0 return]; 96} 97//Gets the last element in the vector 98 t&Back () {return start[finish-start];100}101 const t& Back () const{102 Return Start[finish-start];103}104 void Pushback (const t&x) {105Capacity (); 106//Start[finish-start + 1] = x;107 Start[finish-start] =x;108 finish++; 109}110 voidPopback () {111 if (!Empty ()) {finish--; 113}114}115//Insert element in pos position x116 Iterator Insert (Iterator pos, const t&x) {117 for (size_t index = size (); index >= (size_t) (Pos-start); index--) {118 Start[index + 1] =start[index];119}120 *pos =x;121 finish++; 122 returnPos;123}124//delete element above POS position 125Iterator Erase (Iterator Pos) {126 for (size_t index = (size_t) (Pos-start); index < size (); index++) {127 Start[index] = Start[index + 1];128}129 finish--;pos;131}132//assigns vector n values to elements of x 133 void Assign (size_t n, const t& x) {134 if (n > Endofstorage- start) {135 finish = start + n;136 Capacity (); 137 for (size_t index = 0; Index < n; index++ ) {138 Start[index] = x;139 }140 }141 else {142 for (size_t index = 0; index < n ; index++ ) 143 Start[index] = x;144 }145 finish = start + n;146 }147 public :
//Own tube Expansion 148 void capacity () {149 if (finish >= endofstorage) {size_t capacity = 2 * (Endofstorage-start) + 3 ; 151 Iterator tmp = new t[capacity];
//Copy element My_memcopy (tmp, start, sizeof (T) * (Endofstorage- Start)); 154 size_t ret = finish- start;155 Delete start;156 start = tmp;157 finish = start + ret;15 8 Endofstorage = start + capacity;
//
/*iterator pos = start;
size_t index = 0;
while (Pos < endofstprage)
temp[index++] = *pos++;
Deleta[] start;
start = temp;
finish = start + index;
Endofstorage = start + capacity;*/
159}160}161 void Swap (vector<t>& v) {162 Std::swap (Start, V.start), 163 Std::swap (finish, v.finish); 164 Std::swap ( Endofstorage, v.endofstorage); 165 }166 void Print () {167 for (size_t i = 0; i < size (); i++ ) 168
{169 cout << start[i] << "
; }171 cout << Endl; 172 }173 void* my_memcopy (void* dest, const void* src, size_t sz) {174//assert (!dest | |!src); 175 assert (des T! = NULL | | src! = NULL); 176 char* ret = (char* ) dest;177 char* tmp = (char* ) src;178 while (sz--) {179 *ret = * tmp;180 ret++ ; 181 tmp++ ; 182 }183 return dest;184 }185 private : 186 Iter Ator start;187 Iterator finish;188 Iterator endofstorage;189};
, the commented out part of the code is the pit I've stepped on, and here's some test code
1 #include "vector.h" 2 voidTest1 (3){4 vector<int>List1; 5 List1. Pushback (1); 6 List1. Pushback (2); 7 List1. Pushback (3); 8 List1. Pushback (4); 9 List1. Pushback (5); 10List1. Print (); vector<int>list2;12 List2. Pushback (0); list2. Pushback (9); List2. Pushback (8); List2. Pushback (7); List2. Pushback (6); List2. Print (); list1 = list2;19 list1. Print (), }21 void Test2 (), {vector<int> list1;24 list1. Pushback (1); List1. Pushback (2); List1. Pushback (3); List1. Pushback (4); List1. Pushback (5); List1. Print (); List1. Popback (); List1. Print (); List1. Insert (&list1. At (2), 0); List1. Print (); List1. Erase (&list1. at (3)); List1. Print (); }37 int main () Test1 (); Test2 (); 0 GetChar (); 1>
Note: the my_memcpy () function in the function of capacity expansion is essentially a copy of the value, and there is no problem when the built-in type is stored in the vector, but problems like the string class cannot be solved. So here's another way to write.
/*iterator pos = start;
size_t index = 0;
while (Pos < endofstprage)
temp[index++] = *pos++;
Deleta[] start;
start = temp;
finish = start + index;
Endofstorage = start + capacity;*/
C + + templates-Implement container adapters