DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

std::vector c++

vector:

This is DYNAMIC ARRAY and most widely used container, We use this data structure when we don’t know the number of element in advance or compile time, number of element would vary during run time then we will use vector.

Header File:

#include<vector>

When we will use vector:

  • want to handle dynamic element.
  • want to do insert and delete operation at the end of container (performance is best in this case).
  • number of element are not huge.
  • want fast and random access the element.

Advantage: 

  • Fast access (because store the element in continuous memory)
  • Random access (if know the position then directly jump to that location because it stores the element in continuous memory)
  • Direct access using subscript operator [] (if know the position of element)

Disadvantage:

  • It stores element in continuous memory so memory might be full at some point.
  • Can not handle large number of element because store in continuous fashion.
  • Performance is worst when insert and delete operation happen at beginning or middle.

 

Source code of std::vector with example:

/*
Program:std::vector
Author: Alpha Master
Date: 23 Aug 2021
*/

//Header File
#include<iostream>
#include<vector>
#include<algorithm>

void display(std::vector<int>& vec)
{
    //Display
    std::vector<int> :: iterator itr;
    for(itr = vec.begin(); itr != vec.end(); itr++)
    {
        std::cout<<*itr<<" ";
    }
    std::cout<<std::endl;
}


int main()
{
    std::cout<<"std::vector"<<std::endl;
    
    //Create vector object
    std::vector<int> vec;
    std::cout<<"size:"<<vec.size()<<std::endl;

    //Add element in vector
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    vec.push_back(4);
    vec.push_back(5);
    std::cout<<"size:"<<vec.size()<<std::endl;

    //Access element
    //Random access using [] operator
    std::cout<<"First element:"<<vec[0]<<std::endl;
    std::cout<<"Third element:"<<vec[2]<<std::endl;
    std::cout<<"Fourth element:"<<vec.at(3)<<std::endl;

    //find
    if(std::find(vec.begin(), vec.end(), 4) != vec.end())
    {
	std::cout<<"Found"<<std::endl;
    }

    //find and not present  
    if(std::find(vec.begin(), vec.end(), 9) != vec.end())
    {
        std::cout<<"Found"<<std::endl;
    }
    else
    {
        std::cout<<"Not Found"<<std::endl;
    }

    //Display
    display(vec);
 
    //Remove
    std::vector<int> :: iterator itr;
    for(itr = vec.begin(); itr != vec.end(); itr++)
    {
        if(*itr == 3)
	{
            std::cout<<"Remove 3"<<std::endl;
	    vec.erase(itr);
            break;
	}
    }

    //Display
    display(vec);

    return 0;
}

Output:

std::vector
size:0
size:5
First element:1
Third element:3
Fourth element:4
Found
Not Found
1 2 3 4 5 
Remove 3
1 2 4 5 

Implement Own Vector Class in C++

Primary Sidebar

DigestCPP © 2022. All rights reserved.

    Contact Us   Disclaimer