In: Computer Science
{4, 6, 12, 15, 19, 23, 24}
Write one statement to print out (cout) the first character and the last character of the vector you initialized
Write one statement to insert an integer 9 to myvec between numbers 6 and 12.
in c++
PROGRAM :
#include <iostream>
// <bits/stdc++.h> for STL libraries
#include <bits/stdc++.h>
using namespace std;
int main() {
// initialising vector
vector<int> vect{4, 6, 12, 15, 19, 23, 24} ;
// vector.front() and vector.back() returns first and last element of vector respectively
cout << "First and last element : " << vect.front() << " and " << vect.back() << "\n";
// 9 is to be inserted between 6 and 12 ,thus the index to be inserted is 2
// vector.insert(position,value) inserts the value ate the specified position
vect.insert(vect.begin() + 2 ,9);
for (int x : vect)
cout << x << " ";
cout << "\n";
return 0;
}
OUTPUT: