STL: Standard Template Library
Standard Template Library
The STL:
Sequence containers: random data
vector array
list doubly linked
deque bidirectional dynamic queue
relationship container: Ordered Data
Map
SET
the multimap
multiset
has a function of container: CRUD
has container functions:
structure, destructor, insertion , delete
search, copy construction, the number of elements ......
iterator: return address
iterator: a container used to locate an element in
the array: index
list: next pointer
container: smart pointers
returned by the iterator value: yes address elements in the container
thus is preferably used to initialize each begin ()
iterator .begin (): the address points to the first element of the
iterator .end (): points to the last element of the
container .insert (iterators, data ): insert
if the container is changed, the iterator will fail
List: dynamic bidirectional linked list
arrays && list: efficient array search
list efficient insertion and deletion
array of continuous, discontinuous list
bidirectional dynamic queue: the deque
of between arrays and linked lists
Reference Code:
# include<iostream> # include<vector> using namespace std; int main () { vector <int> v; // create a template class vector <int> :: iterator it; // create an iterator object v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); // cout << v [0] << endl; // read the first position reading a first position //cout<<v.at(0)<<endl;// // Read all the elements for(int i=0; i<v.size(); i++){ cout<<v.at(i)<<" "; } cout<<endl; //v.pop_back();// delete the last // Use the iterator // iterator pointing to the beginning of the first element it = v.begin(); it + = 3; // 1 + 3: iterator to the fourth element v.erase (it); // delete iterator pointing to the element // read all elements using an iterator //it=v.begin (): initialize the iterator for(it=v.begin(); it!=v.end(); it++){ cout<<*it<<" "; } cout<<endl; // insert elements: insert Use it = v.begin(); it + = 2; // points to third position v.insert (it, 9627); // third position of the insert for(it=v.begin(); it!=v.end(); it++){ cout<<*it<<" "; } cout<<endl; return 0; }