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 don’t have to specify the size of the array before hand. There are many methods to manipulate the data in a vector:
You 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<RomanType> myVector;
myVector.push_back(RomanType(“MCM”)); // 1900
myVector.push_back(RomanType(“CCCLIX”)); // 359
myVector.push_back(RomanType(“MDCLXVI”)); // 1666
sort(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;
}