standard-library-in-x

Notes and readings for STL workshop

View on GitHub

Vectors

INTRODUCTION

The simplest STL container is vector.
Vector is just an array with extended functionality. In other words, vectors are dynamic arrays.

CONSTRUCTION

vector<datatype> a;  //empty vector
vector<datatype> b (no of elements, value of each element); //fixed number of elements with default value
vector<datatype> c (starting iterator/pointer,ending iterator/pointer); //inserting elements from other data structures
vector<datatype> d (name of vector to be copied);    
vector<vector<datatype> > matrix(no of rows,vector<datatype>(no of coloumn,default value)) //Declaring a 2D array

// Note:
vector<datatype> v[10]; // following declaration isn't a vector with 10 elements but an array of size ten having vector elements


FUNCTIONS

Function
What it does ?
Complexity
at() Returns the reference to the element at a particular position (can also be done using ‘[ ]’ operator) O(1)
clear() Deletes all the elements from the vector and assign an empty vector O(N)
pop_back() Removes the last element from the vector O(1)
push_front() Inserts a new element at the end of the vector O(1)
front() Returns reference of the first element O(1)
back() Returns reference of the last element O(1)
empty() Returns 1 if vector is empty, else 0 O(1)
size() Returns the total no of elements in the vector O(1)
resize() Resizes the vector to the new length which can be less than or greater than the current length O(N)

Note

begin() and end() function return an iterator(like a pointer) initialized to the first or the last element of the container that can be used to iterate through the collection, while front() and back() function just return a reference to the first or the last element of the container.

PASSING AS ARGUMENT TO FUNCTION

Another very important thing that you should remember: When vector is passed as a parameter to some function, a copy of vector is actually created. It may take a lot of time and memory to create new vectors when they are not really needed.

void some_function(vector<int> v) { // Never do it unless you’re sure what you do!
      // ...
 }

Instead, use the following construction:

int modify_vector(vector<int>& v) { // Correct
      V[0]++;
 }

Implementation

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector <int> v;
    v.push_back(5);
    while(v.back() > 0)
        v.push_back(v.back() - 1);
    for(int i = 0;i < v.size();++i)
        cout << v.at(i) << ' ';
    cout << endl;
    while(!v.empty())
    {
        cout << v.back() << ' ';
        v.pop_back();
    }
    cout << endl;
    return 0;
}

Output:

5 4 3 2 1 0
0 1 2 3 4 5


Further much more functionality can be understood once we start using iterators