Write a C++ program based on the STL vector to process each operation in the seq
ID: 3680813 • Letter: W
Question
Write a C++ program based on the STL vector to process each operation in the sequence shown in the attached photo. After each operation, print the operation and the result. The photo also provides the results one should be getting.
Using the STL vector, creete a vector and invoke the built-in functions necessary for opergtions shown below. After each operation, print the ouput and vector contents. Operation Output insert(1,7) insert(0,4) … at( 1) insert(2,2) at(3) erase(1) insert(1,5) insert(13) - insert(4,9) at(2) set(3,8) (4,7) (4,7) (4,7,2) (4,7,2) (4,2) (4,5,2) (4,3,5,2) (4,3,5,2,9) (4,3,5,2,9) (43,5,8,9) error"Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// create a vector to store int
vector<int> vec;
int i;
vec.insert(1,7);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
vec.insert(0,4);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
cout<<vec.at(1);
vec.insert(2,2);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
cout<<vec.at(3);
vec.erase(1);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
vec.insert(1,5);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
vec.insert(1,3);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
vec.insert(4,9);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
cout<<vec.at(2);
vec.set(3,8);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << ' ';
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.