www.openskytechnology.comDescription: Description: D:\website\syllabus\study_matreials\c++\logo_pic\logo.jpg

Training Home

Vector

  _______________________________________________________________________

 

Vector: Vectors are sequence containers. Vectors are implemented using dynamic array. So, vector has its elements stored in contiguous memory location. Any vector element can be accessed randomly using the index. Also, using the iterators the vector elements can be accessed as well.

          Unlike regular arrays, storage in vector is handled automatically, allowing it to be expanded and constructed as needed.

 

Vectors are good at:

·         Accessing individual elements by their position index (constant time).

·         Iterating over the elements in any order (linear time).

·         Add and remove elements from its end (constant amortized time).

 

Example: Usage of Vector

#include <iostream>

#include <vector>

#include <cassert>

using namespace std;

int main()

{

    vector<int> v;

    v.push_back(5);

    v.push_back(3);

    v.push_back(6);

    v.push_back(9);

    v.push_back(4);

    for (vector<int>::iterator it = v.begin(); it != v.end(); it++)

    {

   cout<<*it<<" ";

    }

    cout<<endl;

}

 

_______________________________________________________________________

 

 

Copyright © Open Sky Technology

 

Corporate Office:  #4, RR Complex, 2nd Floor, Munnekolala, Marathahalli, Bangalore – 560037. (Landmark: near Munnekolala Bus Stop)   Mobile: 9886333765,