Respond to both discussion posts about vector containers discussing how to manipulate data stored in a vector container. From John: Vectors from the C++ STL are much like arrays, but much more convenient. Unlike arrays, you dont have to specify the size of the array before hand. There are many methods to manipulate the data in a vector:push_back: appends an item to the end of the vectorpop_back: removes and returns the item at the end of the vectorat, []: returns the item at the specified indexbegin: returns an iterator to the beginning of the vectorend: returns an iterator to the end of the vectorrbegin: returns a reverse iterator to the end of the vector (reverse beginning)rend: returns a reverse iterator to the beginning of the vector (reverse end)size: returns the size of the vectorclear: empty the vectorYou can iterate through a vector using a standard for loop (with an indexor). For example:for (int i = 0; i < myVector.size(); i++) { auto item = myVector.at(i);}Or, I prefer to iterate over vectors using begin and end:for (auto i = myVector.begin(); i != myVector.end(); i++) { auto item = (*i);}Using the iterator methods is particularly useful for iterating through the vector in reverse:for (auto i = myVector.rbegin(); i != myVector.rend(); i++) { auto item = (*i);}And,
using std::sort we can easily sort the items of a vector:vector myVector;myVector.push_back(RomanType(MCM)); // 1900myVector.push_back(RomanType(CCCLIX)); // 359myVector.push_back(RomanType(MDCLXVI)); // 1666sort(myVector.begin(), myVector.end(), [](RomanType a, RomanType b) { return a.toInt() < b.toInt();});for (auto i = myVector.begin(); i != myVector.end(); i++) { cout << (*i).toString() << endl;}// Output:// CCCLIX// MDCLXVI // MCM From Steve: There are various operators to manipulate a vector, from clearing, erasing, inserting or resizing the vector. As far as erasing or clearing the vector vectorName.clear() or vectorName.erase(position) will delete all contents or delete content as a specified location. Inserting items to a vector can be accomplished with vectorName.insert(position, elem) or vectorName.push_back(elem), for which a copy of the element will be inserted at the position defined or inserted at the end of the vector. The vector may also be dynamically be resized unlike with arrays. Once an array is defined it may not change it size. The operations for resizing a vector are vectorName.resize(num) or vectorName.resize(num,elem) which when increased changed the elements to num and the default constuctors automatically increase the new elements.