// test vector class // #include #include using namespace std; int main( void) { // initialize vector from an array // int a[] = { 1, 2, 3}; cout << "a = "; for( int x : a) { cout << " " << x; } cout << endl; // vector v( a, a+3); // initializer list, see https://en.cppreference.com/w/cpp/language/list_initialization // vector v { 1, 2, 3}; // print using range-based for loop // cout << "v = "; for( int x : v) { cout << " " << x; } cout << endl; cout << "v = "; for( const auto& x : v) { cout << " " << x; } cout << endl; // print using v.at() indexing // cout << "v = "; for( size_t i = 0; i < v.size(); ++i) { cout << " " << v.at(i); } cout << endl; // print using iterator (it is something like a pointer to int) // cout << "v = "; for( vector::const_iterator it = v.begin(); it != v.end(); ++it) { cout << " " << *it; } cout << endl; // append another element // v.push_back(4); cout << "v = "; for( int x : v) { cout << " " << x; } cout << endl; // erase v[1] and v[2] // vector::iterator it = v.begin() + 1; // points to v.at(1) v.erase( it, it+2); cout << "v = "; for( int x : v) { cout << " " << x; } cout << endl; }