[prog] C++ STL at() function

ed orphan millward at Ms.UManitoba.CA
Sun May 11 13:59:01 EST 2003


As I understand it, the Standard Template Library function
at() is used to insert an element into a vector and also
to check for an out_of_range error.  Sounds good, and it
would be really useful, but I can't get it to work.
The compile time GNU error is:
 no matching function to call to
"vector<int>, allocator<int>>::at(int)"    duh?
I'm using the GNU C++ compiler on Red Hat 7.3.
g++ -Wall -W -o at_test at_test.cpp
  Could someone tell me what I'm doing wrong?
Here is the code ( as short as I can make it )
#include <iostream>
#include <vector>
#include <algorithm>  // not sure I need this
#include <stdexcept>  // for out_of_range

int main() {
   int apple[ 6 ] = { 1,2,3,4,5,6 };
   vector< int > v ( apple, apple + 6 ); // create and initialize vector v
   try {
       v.at( 100 ) = 66;   // deliberate error
   }
   catch( out_of_range e )
   {
     cout << "\nError  " << e.what();
   }
   return 0;
}



More information about the Programming mailing list