Vector and Iterator in C++



PROGRAM:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
        vector<int> myarr;

        cout << "\nSize : " << myarr.size() << endl;

        //put data at vector
        myarr.push_back(5);
        myarr.push_back(10);
        myarr.push_back(15);
        myarr.push_back(20);
        myarr.push_back(25);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //delete first value
        myarr.erase(myarr.begin());

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //delete last value
        myarr.erase(myarr.end()-1);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //insert at position
        myarr.insert(myarr.begin()+2, 100);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        //delete middle value
        myarr.erase(myarr.begin()+1);

        cout << "\nSize : " << myarr.size() << endl;
        for (int i = 0; i < myarr.size(); i++)
                cout << "\tArray values " << myarr[i] << endl;

        return 0;
}


OUTPUT:

Size : 0

Size : 5
        Array values 5
        Array values 10
        Array values 15
        Array values 20
        Array values 25

Size : 4
        Array values 10
        Array values 15
        Array values 20
        Array values 25

Size : 3
        Array values 10
        Array values 15
        Array values 20

Size : 4
        Array values 10
        Array values 15
        Array values 100
        Array values 20

Size : 3
        Array values 10
        Array values 100
        Array values 20


Introduction to GNU profiler

Introduction:

Profiling is the process of finding the time complexity of program.

Profiler will find the time taken by the parts of the code. It will help to programmer
to reconstruct the code if the parts of the code consumed more time.

There is many profiling tools available. But this article give idea on native GNU profiler(gprof)

Step 1:

Write a simple program called proftest.c

#include <stdio.h>
void firstFunction(int );
void secondFunction(int );

int main(int argc, char *argv[])
{
        int a = 10;
        int *b = &a;
        firstFunction(a);
        return 0;
}

void firstFunction(int a)
{
        a += 10;
        sleep(1);
        secondFunction(a);
        return;
}

void secondFunction(int b)
{
        sleep(5);
        printf("final value : %d\n", b);
        return;
}

step 2:
Compile the program with '-pg' option using gcc

gcc -pg proftest.c -o profout

Step 3:
Execute the program

./profout

It will create the gmon.out file in current directory

step 4:
run the gprof with gmon.out

 gprof -b profout gmon.out

You will get the output as below

Flat profile:

Each sample counts as 0.01 seconds.
 no time accumulated

  %   cumulative   self              self     total
 time   seconds   seconds    calls  Ts/call  Ts/call  name
  0.00      0.00     0.00        1     0.00     0.00  firstFunction
  0.00      0.00     0.00        1     0.00     0.00  secondFunction

                        Call graph


granularity: each sample hit covers 2 byte(s) no time propagated

index % time    self  children    called     name
                0.00    0.00       1/1           main [7]
[1]      0.0    0.00    0.00       1         firstFunction [1]
                0.00    0.00       1/1           secondFunction [2]
-----------------------------------------------
                0.00    0.00       1/1           firstFunction [1]
[2]      0.0    0.00    0.00       1         secondFunction [2]
-----------------------------------------------

Index by function name

   [1] firstFunction           [2] secondFunction