Vector Stl in C++

Vector in STL C++

It is just like dynamic array. It' s operation similar as list operation in python.

Declaration

vector <data_type> variable_name;

ex. -
when primitive data structure-
        vector <int> variable_name;

when class or structure type-
      vector <Node*> variable_name;

Method -

  push_back()- Push data into last of the vector.
       Suppose vector is var=[1,2,5,6]
       var.push_back(40)
       after above operation vector becomes var=[1,2,5,6,40]

pop_back()- Pop or remove data from last of the vector.
       Suppose vector is var=[1,2,5,6,40]
       var.pop_back()
       after above operation vector becomes var=[1,2,5,6]

size()- Gives size of the vector.
       Suppose vector is var=[1,2,5,6,40,8]
       s = var.size()
       after above size of vector  s=6.

clear()- Clear or remove all values of vector.
       Suppose vector is var=[1,2,5,6,40,8]
       s = var.clear()
       after above operation vector becomes var=[ ]

empty()- Check whether vector is empty or not. if empty then return true else false.
       Suppose vector is var=[1,2,5,6,40,8]
       s = var.empty()
       after above operation s contains false.

       Suppose vector is var=[ ]
       s = var.empty()
       after above operation s contains true.

erase()-
//remove the first element
s.erase(s.begin()); 

begin()-return pointer address of first element

Access element in vector-
x=s[0]    //gives first element


Simple Example-
  
#include <iostream> 
#include <vector> 
  
using namespace std; 
  
int main() 
    vector<int> v; 
    int x;
    for (int i = 1; i < 7; i++) {
        cin>>x; 
        v.push_back(x);}
  
    cout << "Size = " << v.size()<<"\n"; 
    for (int i = 0; i < 7; i++) {
        cout<<v[i]<<" "; 
        }
    v.pop_back();
    cout<<"\n"<<" after pop operation "<<"\n";
    for (int i = 0; i < v.size(); i++) {
        cout<<v[i]<<" "; 
        }
    cout<<"\n"<<" Check if is empty  "<<v.empty()<<"\n";
    v.clear(); 
  cout<<" After clear operation check if is empty  "<<v.empty()<<"\n";
    



Complex Program- Level order traversal of tree in spiral form.
             Suppose tree is   
                                1
                               /   \
                             2     3
                           /    \      \
                          8      9     6
                         /   \          /   \
                      12    7     15     5

    Spiral traversal is 1 2 3 6 9 8  12 7 15 5.

  Solution-

void printSpiral(Node *root)
{
    //Your code here
    vector <Node*> fstack,sstack;
    
    Node *cur=root;
    if(!root) {
        cout<<"";
        return;
    }
    int f=-1;
    string res=to_string(root->data);
    fstack.push_back(root);
    while (cur && !fstack.empty()){
        while(!fstack.empty()){
            cur=fstack[fstack.size()-1];
            fstack.pop_back();
            if (f==-1 && cur ){
                if(cur->left) {
                    res=res+" "+to_string(cur->left->data);
                    sstack.push_back(cur->left);
                }
                if(cur->right ) {
                    res=res+" "+to_string(cur->right->data);
                    sstack.push_back(cur->right);
                }
            }
            else if (f==1 && cur ){
                
                if(cur->right) {
                    res=res+" "+to_string(cur->right->data);
                    sstack.push_back(cur->right);
                }
                if(cur->left) {
                    res=res+" "+to_string(cur->left->data);
                    sstack.push_back(cur->left);
                }
            }
        }
        fstack=sstack;
        sstack.clear();
        f=(-1)*f;
        
    }
    cout<<res;

}



    


























Comments