STL Vector, List


1. The List ADT


2. Intro to STL


3. Iterators


3.1 Declaring, Obtaining and Moving iterators


3.2 Insertion, Deletion using Iterators

vector<int> vec; 

/** Add some elements in the vector */
vec.push_back(7);  // [7]
vec.push_back(3);  // [7, 3]
vec.push_back(5);  // [7, 3, 5]

/** Insert and elements using iterators */
vector<int>::iterator it;
it = vec.begin();
it++;              // it points to 3
vec.insert(it, 1); // [7, 1, 3, 5]

it = vec.begin();  // reset it to point to the beginning
it++;
it++;  // it points 3

vector<int>::it2, it3;
it2 = vec.erase(it);  // delete 3, it2 points to 5
it3 = vec.erase(it2); // delete 5, it3 points to the end of the list

if (it3 == vec.end())
  cout << "End of the list\n";
else
  cout << "ERROR!!\n";

3.3 Const_iterators


3.4 Comparing iterators


4. STL Algorithms


EXERCISES:

  1. Write a code segment that removes all even integers from an STL vector (of int's).
  2. Write another code segment that reverses an STL list (of int's).
  3. Write a function called myVectorFind which works similar to the STL find algorithm.  Assume the vector contains char's.  The prototype of the function is:

      vector<char>::iterator myVectorFind(vector<char>::iterator start, vector<char>::iterator end, char x);