STL Vector example in C++ in Linux all operations

//Vector is a STL container. Vector is template type container, which can store any type of element by providing type in template arguments. Vector size expanded to double or shrinks dynamically at run time. Just like array memory is allocated contagiously but unlike array it is stored in heap. 
//vector is should be preferred as compared to dynamic array because
//1.memory is automatically freed in case of vector on destruction
//2.vector is implemented internally as dynamic array in heap which
//shrinks and grows automatically so no wastage of memory
//3. It is fast as elements can be accessed randomly by index just like arrays.
For understanding:
No of elt  size  capasity
0              0       0
1              1       1 
2              2       2
3              3       4
4              4       4
5              5       8
6              6       8
7              7       8
8              8       8 
9              9       16
10           10      16
17            17     32 
using namespace std;
int main()
vector<string> myvector;
cout<<"-----"<<"Inserting at end using pushback is efficient in vector as no shift:"<<endl;
cout<<"-----"<<"Size of the vector is:"<<myvector.size()<<endl;

cout<<"-----"<<"Traversing vector with iterator:"<<endl;
vector<string>::iterator it;
cout<<*it<<" ";//mind the syntax to display//cccc bbbb eeee aaaa dddd

cout<<"\n----"<<"Traversing vector with index subscripts:"<<endl;
for(int i=0;i<myvector.size();i++)
cout<<myvector[i];//mind the syntax to display//cccc bbbb eeee aaaa dddd

cout<<"\n-------"<<"Sorting vector elements in ascending order:"<<endl;
cout<<*it<<" ";//aaaa bbbb cccc dddd eeee

cout<<"\n------------"<<"Finding a string in a vector of strings:"<<endl;
int pos;string elt="bbbb";
cout<<"bbbb found at index:"<<pos<<endl;//bbbb at 1st index
cout<<"Not found"<<endl;

cout<<"------------"<<"Vector before erasing is:"<<endl;
for(int i=0;i<myvector.size();i++)
cout<<;//aaaa bbbb cccc dddd eeee

cout<<"\n------------"<<"Erasing 1st index element ie bbbb of vector"<<endl;
myvector.erase(myvector.begin()+1);//deleting 1st index leaving 0th index elt
cout<<"Now vector is:";
for(int i=0;i<myvector.size();i++)
cout<<" "<<myvector[i];//aaaa cccc dddd eeee

cout<<"\n------------"<<"Erasing first 2 elements:"<<endl;
myvector.erase(myvector.begin(), myvector.begin()+2);//2 elements and not 2 index
cout<<"Now vector is:";
for(int i=0;i<myvector.size();i++)
cout<<" "<<myvector[i];//dddd eeee

cout<<"\n------------"<<"clear deletes all the elts of a vector"<<endl;
//Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
for(int i=0;i<myvector.size();i++)

cout<<"------------"<<"Inserting method 2 in vector"<<endl;

cout<<"------------"<<"pop_back in vector to delete from last"<<endl;
cout<<myvector.back();//gggg ffff
}//vector is empty now
cout<<"\nInserting after 0th elt";
vector<string>::iterator itr2=myvector.begin();
for(int i=0;i<myvector.size();i++)
cout<<" "<<myvector[i];//pppp bbbb

cout<<"\nInserting after nth elt";
vector<string>::iterator itr22=myvector.begin();
myvector.insert(itr22+1,"hhhh");//insert hhhh after pppp
for(int i=0;i<myvector.size();i++)
cout<<" "<<myvector[i];//pppp hhhh bbbb

cout<<"\n------------"<<"Finding other way:"<<endl;
vector<string>::iterator itr;
cout<<"pppp found at position:"<<itr-myvector.begin()<<endl;//1
//deleting found element as
cout<<"Not found"<<endl;
cout<<"After deleting pppp vector becomes:::\n";
for(int i=0;i<myvector.size();i++)
cout<<" "<<myvector[i];// hhhh bbbb

cout<<"\n------------"<<"Reverse a vecotr:"<<endl;
for(int i=0;i<myvector.size();i++)
cout<<myvector[i]<<" ";//bbbb hhhh pppp

cout<<"\n------------"<<"insert repeated values"<<endl;
//exchanges elements of vectors even difference in sizes
vector<int> vec1 (3,100);   // three ints with a value of 100
vector<int> vec2 (5,200);   // five ints with a value of 200
for(int i=0;i<vec1.size();i++)
cout<<vec1[i]<<" ";//becomes 200 200 200 200 200

cout<<"\n------------"<<"Delete all occurence of an element from vector"<<endl;
vec1.erase(remove(vec1.begin(), vec1.end(), 200), vec1.end());
for(int i=0;i<vec1.size();i++)
cout<<vec1[i]<<" ";//becomes 200 200 200 200 200

cout<<"\n-------------"<<"Finding and deleting a particular element in a vector."<<endl;
//vector<int> vect3{20,30,40,111,111,11,60};//need to compile with -std=c++11
vector<int> vect3;
int count=0;
for( vector<int>::iterator iter = vect3.begin(); iter != vect3.end(); ++iter )
    if( *iter == 111 )
cout<<"111 found at: "<<count<<" and "<<;
        vect3.erase( iter );
cout<<"\nNow vector is:"<<endl;
for(int i=0;i<vect3.size();i++)
cout<<vect3[i]<<" ";//becomes 20 30 40 111 11 60

cout<<"\n----Find and replace in a vector of integers"<<endl;
int myints[] = { 11, 22, 33, 33, 11, 11, 22, 44,55,66 };
vector<int> myvector2 (myints, myints+10);            // 11 22 33 33 11 11 22 44 55 66
replace (myvector2.begin(), myvector2.end(), 22, 222); // 11 222 33 33 11 11 222 44 55 66

cout << "myvector2 contains:";
for (vector<int>::iterator it=myvector2.begin(); it!=myvector2.end(); ++it)
cout <<" "<< *it;//10 99 30 30 99 10 10 99

return 0;
------------Inserting at end using pushback is efficient in vector as no shift:
------------Size of the vector is:5
------------Traversing vector with iterator:
cccc bbbb eeee aaaa dddd
------------Traversing vector with index subscripts:
------------Sorting vector elements in ascending order:
aaaa bbbb cccc dddd eeee
------------Finding a string in a vector of strings:
bbbb found at index:1
------------Vector before erasing is:
------------Erasing 1st index element ie bbbb of vector
Now vector is: aaaa cccc dddd eeee
------------Erasing first 2 elements:
Now vector is: dddd eeee
------------clear deletes all the elts of a vector
------------Inserting method 2 in vector
------------pop_back in vector to delete from last
Inserting after 0th elt pppp bbbb
Inserting after nth elt pppp hhhh bbbb
------------Finding other way:
pppp found at position:0
After deleting pppp vector becomes:::
 hhhh bbbb
------------Reverse a vecotr:
bbbb hhhh
------------insert repeated values
200 200 200 200 200
------------Delete all occurence of an element from vector

-------------Finding and deleting a particular element in a vector.
111 found at: 4 and 111
Now vector is:
20 30 40 111 11 60

/*Vector is internally implemented as dynamically allocated if size is not know then int *p = new in[size]. Non dynamic array example is int array[SIZE];wastage of unused memory. In dynamic array only pointer to array is static. So vector is used for efficient memory allocation when insert is at last scenarios.
As the vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).*/

1 comment:

  1. Vector is internally implemented using arrays and this array grows dynamically. There is some memory strategy that vector uses when it grows.Vector has two properties
    1) Size() -number of elements in the vector
    2) Capacity(): total amount of memory available to store elements in terms of elements.

    Initially vector will be empty and the space allocated (capacity) is zero.When we keep on adding elements to the vector size increases one by one , but capacity increases not one by one but with some factor. That factor depends upon the
    Compiler used. In gcc compilers it is of the order of 2;ie 1,2,4,8 etc. In MSVC it is different.