// Examples of working with the Standard Template Library (the STL) // // Here is a comparison of C++ structures and corresponding Java structures: // C++ structure Java structure // -------------------------------- // vector ArrayList // list LinkedList // set TreeSet // multiset no equivalent // map TreeMap // pair Map.Entry // // Basic operations on the structure: // function/operator vector list set multiset // --------------------------------------------------------- // push_back(value) x x - - // push_front(value) - x - - // insert(value) - - x x // back() x x - - // front() x x - - // pop_back() x x - - // pop_front() - x - - // x[index] x - - - // size() x x x x // empty() x x x x // clear() x x x x // ==, !=, <, >, <=, >= x x x x // // Use of iterators on the structure: // function/operator vector list set multiset // --------------------------------------------------------- // begin() x x x x // end() x x x x // iterator++ x x x x // iterator-- x x x x // *iterator (const) x x x x // *iterator (reference) x x - - // iterator+n and +=n x - - - // iterator-n and -=n x - - - // erase(iterator) x x x x // insert(iterator, value) x x x x #include #include #include #include #include using namespace std; // returns a string version of the given vector as a comma-separated, // bracketed, list string vector_to_string(const vector & v) { ostringstream out; out << "["; if (v.size() > 0) { out << v[0]; for (int i = 1; i < v.size(); i++) { out << ", " << v[i]; } } out << "]"; return out.str(); } int main() { vector v {9, -8, 13, 9, 6, 12, 42, 17, -8, 200}; cout << vector_to_string(v) << endl; auto itr = v.begin(); *(itr + 5) = 35; cout << *itr << endl; itr = v.end(); cout << *itr << endl; cout << vector_to_string(v) << endl; cout << endl; // print the size of the vector cout << "size = " << v.size() << endl; cout << "size also = " << v.end() - v.begin() << endl; // print out the midpoint cout << *(v.begin() + (v.end() - v.begin()) / 2) << endl; cout << endl; // inserts a -1 in front of every even # cout << "v = " << vector_to_string(v) << endl; for (auto itr = v.begin(); itr < v.end(); itr++) { if (*itr % 2 == 0) { itr = v.insert(itr, -1); itr++; } } cout << "v = " << vector_to_string(v) << endl; cout << endl; // prints the values in reverse order for (auto itr = v.end() - 1; itr >= v.begin(); itr--) { cout << *itr << " "; } cout << endl; // this works for vectors, but for non-vector types, you would instead say: // auto itr = s1.end(); // while (itr != s1.begin()) { // itr--; // cout << *itr << endl; // } // cout << endl; // add some multiple of 3 and then loop to remove all multiples of 3 v.push_back(30); v.push_back(18); v.push_back(3); v.push_back(17); cout << "v = " << vector_to_string(v) << endl; /* for (auto itr = v.begin(); itr < v.end(); itr++) { if (*itr % 3 == 0) { itr = v.erase(itr); itr--; } } */ // the while loop solution is safer because it doesn't rely on // setting an iterator to a position out of range when vector // starts or ends with a multiple of 3 itr = v.begin(); while (itr < v.end()) { if (*itr % 3 == 0) { itr = v.erase(itr); } else { itr++; } } cout << "v = " << vector_to_string(v) << endl; return 0; }