Standard Template Library

Reviews
Shared by: moneu
Stats
views:
19
rating:
not rated
reviews:
0
posted:
11/16/2008
language:
English
pages:
0
Standard Template Library Iterators and Containers Why STL     tasks like implementing a (templated) list of elements arise often in programming practice a library of templated classes that provide a set of templates for common tasks and ways of manipulating them is a part of C++ it is called Standard Template Library (STL) two main constructs  containers – (templated) classes that simplify data manipulation  iterators – ways of accessing elements containers Containers   sequence containers – element access by position in container  vector – indexed access to any element, quick insertions of elements in back only  deque – indexed access, quick insertion/deletion from front/back  list – sequential access only, quick insertion/deletion anywhere associative containers – element access by value (will not study)  example: set - rapid lookup of elements, no duplicates allowed container declaration syntax: container_class container_name; where container_class - vector, deque, list, etc. type_parameter- type/class of elements container_name – identifier example vector items; // declares vector with no elements  Common Container Functions c.empty() const; // true if there are no entries in c c.size() const; // number of entries in container c c.erase(); // removes one or more elements of the container c.clear(); // removes all elements from the container c = v; // replace contents of c with contents of v c.swap(v) // swaps contents of c and v Iterators  iterator is a generalization of pointer  not a pointer but usually implemented using pointers  pointer operations may be overloaded for behaviour appropriate for the container  treating iterators as pointers typically is OK  each container defines an appropriate iterator type declaring iterator container_class ::iterator_type iterator_name; where container_class – vector, deque, list, etc. type_parameter – type/class of elements iterator_type – iterator, const_iterator (more later) iterator_name – identifier example  vector ::iterator p; Iterators Operations  operations  ++ (pre- and postfix) and -- to advance to the next (previous) data item  = = and != operators to test whether two iterators point to the same data item  dereferencing (*) provides data item access member functions    c.begin() returns an iterator pointing to the first element of container c c.end() returns an iterator pointing past the last element of container c – analogous to the null pointer. Unlike the null pointer, you can apply – to the iterator returned by c.end() to get an iterator pointing to last element in the container Example Usage vector l; // declare vector … for ( // declare and initialize iterator vector::iterator p = l.begin(); p < l.end(); // check if end is not reached p++ // move iterator to next element ){ cout << *p; // manipulate element in loop body } Iterator Types  Ordinary iterators are mutable – value can be assigned to the element iterator points to vector ::iterator p; *p = something; // legal constant iterators disallow assignment, makes syntactic error checking easier vector ::const_iterator p; *p = something; // illegal reverse iterator allows traversing container in from end to beginning vector ::reverse_iterator p;  operators ++ and -- have opposite meaning – ++ advances iterator one element back  functions rbegin() and rend() return iterators to end and begin of the container there is reverse constant iterator vector ::const_reverse_iterator p;    Operation Support Operation Function vector List deque Insert at front Insert at back Delete at front Delete at back Insert in middle Delete in middle Sort push_front(e) push_back(e) pop_front( ) pop_back( ) insert(e) erase(iter ) sort( ) X X (X) (X) X X X X X X X - X X X X (X) (X) X (X) this operation is significantly slower.

Related docs
The Standard Template Library
Views: 8  |  Downloads: 2
Standard Template Library
Views: 16  |  Downloads: 1
Standard Template Library
Views: 15  |  Downloads: 1
The Standard Template Library
Views: 14  |  Downloads: 0
Standard Template Library
Views: 4  |  Downloads: 0
Standard Template Library
Views: 5  |  Downloads: 0
Standard Template Library
Views: 28  |  Downloads: 0
Standard Template Library
Views: 186  |  Downloads: 2
The Standard Template Library
Views: 0  |  Downloads: 0
MCSTL The Multi-Standard Template Library
Views: 4  |  Downloads: 0
STANDARD TEMPLATE
Views: 27  |  Downloads: 1
The C++ Standard Template Library
Views: 18  |  Downloads: 2
Other docs by moneu
Transmittal Letter to SEC Enclosing Form_D
Views: 210  |  Downloads: 0
Shareholders Resolution Increasing Capital Stock
Views: 269  |  Downloads: 3
EMPLOYEE BONUS MEMO
Views: 1026  |  Downloads: 8
NOTICE OF APPEAL TO A COURT OF APPEALS
Views: 253  |  Downloads: 0
Response to Preliminary Allegations
Views: 201  |  Downloads: 3
Privacy Policy For Internet Site
Views: 829  |  Downloads: 140
adr101
Views: 123  |  Downloads: 0
2006 Inst CT-1 (PDF) Instructions
Views: 242  |  Downloads: 1
3-D Scanner Competition
Views: 229  |  Downloads: 1