next up previous
Next: The Standard C++ library Up: canned_classes Previous: ifstream and ofstream methods

The Standard C++ Library: vector class

Our compiler comes with an extensive library of classes called the Standard C++ Library. This library is now standard with essentially all C++ compilers, so can now be viewed as an extension of the language. While the Standard C++ ``container'' classes are mainly useful for dealing with lists, stacks, and queues, the vector class is particularly useful in computational science and engineering in cases where convenience is more important than performance. If you would like to look at how the class is defined, you can find an example of just one of the many files in stl_vector.h, but nonexperts will find it very dense reading. We will illustrate its use through examples.

Here is how to use the Standard C++ vector class to read in a list of double precision values without knowing in advance how many there are:

#include <iostream>
#include <vector>
using namespace std;

typedef vector<double> Vec;

int main(){
  Vec a, b;
  double atmp;
  int i;
  
  while(1){
    cin >> atmp;
    if(cin.fail()){
      if(cin.eof())break;
      cerr << "Numeric format error\n.";
      return 1;
    }
    a.push_back(atmp);
  }
  
  cout << "I have read " << a.size() << " values..\n";
  
  for(i = 0; i < a.size(); i++){
    cout << "a[" << i << "] = " << a[i] << "\n";
  }
  b = a;
  
  if(b == a){
    cout << "b and a are identical\n";
  }
  
  b.clear();
  cout << "after clearing b has size " << b.size() << "\n";
  return 0;
}

Here are the new features of this sample code:




using namespace std;

This compiler directive helps prevent conflicts in names of functions and variables. It should be used if you include any of the Standard C++ library headers.




typedef vector<double> Vec;

The vector class takes a suffix to specify the datatype of its components. We define a shorthand type Vec to save typing and to make it easy to change from double to single precision if we choose to later on.




  Vec a, b;

Unlike our own vector class, the Standard C++ class allows us to define a vector without specifying its size. The object that is created behaves like a list of vector components. Initially the list is empty.




  while(1)

The logical expression 1 is always true. So this loop goes on indefinitely until the break is encountered.




    if(cin.fail()){
      if(cin.eof())break;
      cerr << "Numeric format error\n."
      return 1;
    }

We are using methods of the cin class to check for an end of file condition. So the loop stops when we reach the end of the data or we encounter an error. We complain if the failure was not caused by an end of file.




    a.push_back(atmp);

The vector object behaves like a list. We add values to the end of the list with the push_back method. Here we have read a value using cin into the temporary atmp and we are adding it to the end of the list of vector components. The Standard C++ vector class allows its objects to grow dynamically.




  cout << "I have read " << a.size() << " values..";

The size method returns the number of vector components.




     cout << "a[" << i << "] = " << a[i] << "\n";

Subscripting works as expected.




  b = a;

Assignments work as expected.




  if(b == a){
  }

The equivalence == and inequivalence != comparisons behave as expected.




  b.clear();

Another useful method is clear(), which removes all elements, leaving an empty vector.


next up previous
Next: The Standard C++ library Up: canned_classes Previous: ifstream and ofstream methods
Carleton DeTar 2008-11-10