// ========================================================================== // $Id: int_std_vector.cpp,v 1.2 2010/09/20 15:55:08 jlang Exp $ // CSI2372 example Code for lecture 3 // ========================================================================== // (C)opyright: // // Jochen Lang // SITE, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.site.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@site.uottawa.ca // ========================================================================== // $Log: int_std_vector.cpp,v $ // Revision 1.2 2010/09/20 15:55:08 jlang // Improved output and names // // Revision 1.1 2010/09/20 15:51:04 jlang // Added basic std::vector example // // // ========================================================================== #include #include using std::cout; using std::endl; using std::vector; /* * Basic use of stl::vector * * This is just a demo -- better usage later on. */ void manipulatePrint( std::vector iVec_copy ) { cout << "Manipulate and print iVec_copy:" << endl; // loop over the elements for ( vector::iterator iter = iVec_copy.begin(); iter != iVec_copy.end(); iter++ ) { (*iter)++; // Increment the content at *iter cout << *iter << " "; // to console } cout << endl; return; } int main( int argc, char* argv[] ) { vector iVec(10,0); // Make an integer vector of size 10 and // init to 0 int num = 0; // loop over the elements and set them to their rank // using an iterator cout << "Original Sequence: " << endl; for ( vector::iterator iter = iVec.begin(); iter != iVec.end(); iter++ ) { *iter = num++; cout << *iter << " "; } cout << endl; // Copy the vector to another vector vector oIVec = iVec; // Equal compare the vectors if ( oIVec == iVec ) { cout << "oIVec is the same as iVec" << endl; } // Pass the vector to a function manipulatePrint( iVec ); // Not equal compare the vectors if ( oIVec != iVec ) { cout << "Now iVec has changed." << endl; } else { cout << "iVec Sequence unchanged: " << endl; for ( vector::iterator iter = iVec.begin(); iter != iVec.end(); iter++ ) { cout << *iter << " "; } cout << endl; } return 0; }