Introduction to STL Containers expose the iterator interface and the Algorithms work on the iterators . std::vector Example of iterating through a vector is below. Iterator behaves like an enhanced pointer. # include <iostream> using namespace std ; int main ( ) { vector < int > nums ; nums . push_back ( 3 ) ; nums . push_back ( 1 ) ; nums . push_back ( 2 ) ; vector < int > :: iterator first = nums . begin ( ) ; vector < int > :: iterator onePastLast = nums . end ( ) ; for ( vector < int > :: iterator i = first ; i != onePastLast ; i ++ ) { cout << "Next Element is " << * i << endl ; } //sorting a vector sort ( first , onePastLast ) ; //after sorting 1,2,3 } Types of containers Sequence containers(array and linked list): vector, deque, list, forward list, and array Associative containers (binary tree) : set, multiset, map...
Comments
Post a Comment