Consider the list of integers (4,5,6,7,8) and we are implementing this list using an Array.
Do the following operations on the given list in C++ language:
· Add(9): Using Add() to add an element 9 between 6 and 7.
· Next(): Using Next() to move current pointer 1 shift to the right.
· Remove(): To remove an element 7.
· Length(): To return size of the list.
· find(7): traverse the array until 7 is located.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v = { 4,5,6,7,8};
auto it = v.insert(v.begin()+3, 9);
cout << "The vector elements are: ";
for (auto it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
vector<int>::iterator it1;
it = v.begin()+4;
v.erase(it);
cout<<endl;
for (auto it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
return 0;
}
Comments
Leave a comment