Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the STL vector, create a vector and invoke the built-in functions necessar

ID: 3670868 • Letter: U

Question

Using the STL vector, create a vector and invoke the built-in functions necessary for operations shown below. After each operation, print the output and vector contents

insert(1,7)       ---                   (7)

insert(0,4)       ---                  (4,7)

at(1)                7                   (4,7)

insert(2,2)       ---     (4,7,2)

at(3) “error”     (4,7,2)

erase(1) ---       (4,2)

insert(1,5) ---     (4,5,2)

insert(1,3) ---    (4,3,5,2)

insert(4,9) --- (4,3,5,2,9)

at(2)    5 (4,3,5,2,9)

set(3,8) --- (4,3,5,8,9)

Explanation / Answer

Please find the required code below :

#include <iostream>
#include <vector>
using namespace std;

void printVec(vector<int> vec){
// use iterator to access the values
vector<int>::iterator v = vec.begin();
while( v != vec.end()) {
cout << *v <<"," << flush;
v++;
}
cout <<"" << endl;;
}

int main()
{
// create a vector to store int
vector<int> vec(10);

vec.insert(vec.begin()+1, 7);
printVec(vec);
vec.insert(vec.begin(), 4);
printVec(vec);
cout << "vec.at(1) = " << vec.at(1) << endl;
printVec(vec);
vec.insert(vec.begin()+1, 7);
printVec(vec);
cout << "vec.at(3) = " << vec.at(3) << endl;
printVec(vec);
vec.erase(vec.begin()+1);
printVec(vec);
vec.insert(vec.begin()+1, 5);
printVec(vec);
vec.insert(vec.begin()+1, 3);
printVec(vec);
vec.insert(vec.begin()+4, 9);
cout << "vec.at(3) = " << vec.at(2) << endl;
printVec(vec);
vec.at(3)=8;
printVec(vec);


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote