Write a sample vector stl program in C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
main()
{
vector<string> SS;
SS.push_back(“The number is 10″);
SS.push_back(“The number is 20″);
SS.push_back(“The number is 30″);
cout << “Loop by index:” << endl;
int ii;
for(ii=0; ii < SS.size(); ii++)
{
cout << SS[ii] << endl;
}
cout << endl << “Constant Iterator:” << endl;
vector<string>::const_iterator cii;
for(cii=SS.begin(); cii!=SS.end(); cii++)
{
cout << *cii << endl;
}
cout << endl << “Reverse Iterator:” << endl;
vector<string>::reverse_iterator rii;
for(rii=SS.rbegin(); rii!=SS.rend(); ++rii)
{
cout << *rii << endl;
}
cout << endl << “Sample Output:” << endl;
cout << SS.size() << endl;
cout << SS[2] << endl;
swap(SS[0], SS[2]);
cout << SS[2] << endl;
}
What are Vectors modifier and element access
Element access:
operator[] Access element (public member function)
at Access element (public member function)
front Access first element (public member function)
back Access last element (public member function)
Modifiers:
assign Assign vector content (public member function)
push_back Add element at the end (public member function)
pop_back Delete last element (public member function)
insert Insert elements (public member function)
erase Erase elements (public member function)
swap Swap content (public member function)
clear Clear content (public member function)
Allocator:
get_allocator Get allocator (public member function)
