Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Day Class Notes

Download Compiler

Evening Class Notes

Lab Notes

Linux For Windows

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
The vector<T> Class template

*** Note: the vector<T> class template is part of the Standard Template Library.  See this web page for additional information on topics in this memo. ***

The vector<T> class template is a type-independent pattern for a self-contained array class whose capacity may vary.  It’s use is illustrated by the following definitions

    vector<double> realVector;  // creates a vector object with double data members
  vector<string> stringVector; // creates a vector object with string data members

The capacity of a vector object is the number of data values it can hold (can be increased as necessary).
The size of a vector object is the number of data values it actually holds.

Defining vector<T> objects

There are three forms for defining vector<T> objects:

   1. vector<element_type> object_name;
   2. vector<element_type> object_name(initial_capacity);
   3. vector<element_type> object_name(initial_capacity, initial_value);

Form 1 creates object_name with capacity and size 0.
Form 2 creates object_name with capacity and size both equal to initial_capacity;
Form 3 initializes objectname to initial_value (for all elements).

Examples:
      vector<int> intVector(100);
   vector<float> realVector(50, 1.0);
   vector<int> intList;

vector<T> member functions

v.capacity()            Return the number of values v can store
v.size()                Return the number of values v currently contains
v.empty()               Return true if and only if v contains no values
v.reserve(n);           Grow v so that its capacity is n (does not affect v’s size)

v.push_back(value);     Append value at v’s end
v.pop_back();           Erase v’s last element

v.front()               Returns v’s first element
v.back()                Returns v’s last element

vector<T> Operators

Operator  Description

v[i]             Access the element of v whose index is i
v1 = v2      Assign a copy of v2 to v1
v1 == v2    Return true if and only if v1 has the same values as v2, in the same order
v1 < v2      Return true if and only if v1 is lexicographically less tha v2

Vector<T> I/O

Implement the following functions to perform I/O

Print to screen:

template <class T>
void Print(vector<T>& vec)
{
  for (int i = 0; i < vec.size(); i++)
     cout << vec[i] << endl;
}

Read from ifstream:

template <class T>
void Read(istream & in, vector<T>& vec)
{
  T inputValue;

    in >> inputValue;

  while(  !in.eof() )
  {
    vec.push_back(inputValue);

    in >> inputValue;
  }
}

Examples:

Read(cin, realVector); // reads values into the object
Print(realVector); // prints object to terminal

Iterators

Iterators are objects that can “point at” an element in a vector.  There are operations that permit the iterator to move from one element in the vector to another and to access the value of the element being pointed at.

begin() and end() are member functions that return iterators.  For example, v.begin() is an iterator that points to the beginning of vector v.

Defining iterator objects

  vector<double>::iterator vecIter = v.begin();

Defines an iterator named vecIter that initially points to the beginning of vector v.
 

Iterator Operations

Increment operator ++  increments the iterator.  Example:  vecIter++

Decrement operator --  decrements the iterator.  Example vecIter—

Dereferencing operator * accesses the value stored at the position to which the iterator points.
Example: *vecIter

vector<T> Member Functions Involving Iterators:

v.begin()                 Return an iterator positioned at v’s first value

v.end()                   Return an iterator positioned 1 element past v’s last value

v.insert(pos, value)      Insert value into v at iterator position pos
 

Example:  print the elements of the vector using

 Subscript notation

        for (i = 0; i < v.size(); i++)
      {
          cout << v[i] << " ";
      }

 Iterator notation

         vector<double>::iterator vecIter = v.begin();

      while(vecIter != v.end() )
      {
          cout << *vecIter <<  " ";
          vecIter++;
      }

 

Algorithms

Note: use #include <algorithm>

STL provides many standard algorithms for use with the containers.  Here we describe the find() algorithm to find an elemnt within a vector.

To find a value x in vector v, do:

      vector<T>::iterator location;

    location = find( v.begin(), v.end(), x);

find() returns a iterator that is either pointed at the first element of v containing value x or indicates the end of the sequence.  Then we can do the following:

     if( location != v.end() )
       cout << "value found" << endl;
    else
       cout << "value not found" << endl;

 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming