Responding to discussion posts about vector containers
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:
- push_back: appends an item to the end of the vector
- pop_back: removes and returns the item at the end of the vector
- at, []: returns the item at the specified index
- begin: returns an iterator to the beginning of the vector
- end: returns an iterator to the end of the vector
- rbegin: 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 vector
- clear: empty the 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;
}
// 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.
10 years ago
8
Purchase the answer to view it

- standard_template_library.docx
- NUR 518 Week 4 Qualitative Research Article Analysis
- Summery and Response
- 2 pages describing the water quality issue in State of Georgia
- Supply Chain Design and Implementation
- Writing a press release HW
- BIO 315 Week 2 Individual Assignment Environment Resources and Competition
- Write a four page paper evaluating the relationship bewteen motivation, job satisfaction, and work performance. Be sure to address the following: a. Describe a performance issue which resulted from a motivational problem ( what, why, who). b. Use content
- need power point with each of these titles below
- Math Probabilities (P2)
- Marketing Plan