Tuesday, 13 January 2015

vector stl

  • INCREASING THE SIZE OF 2ND DIMENSION IN 2D ARRAY
  • PASSING 2D VECTOR TO A FUNCTION
#include<stdio.h>
#include<vector>
using namespace std;
int check(vector < vector <int > > g)
{
    for(int i=0;i<g.size();i++) /* it all becomes more easy since vector knows its size whereas array doesnt in a cpp program */
    {
        for(int j=0;j<g[i].size();j++)
        {
            printf("%d ",g[i][j]);
        }
        printf("\n");
    }

}
int main()
{
vector< vector<int> > g;
int t;

for(int i=0;i<3;i++)
{
    g.push_back(vector<int>());// increasing the size of 2nd dimension 
    for(int j=0;j<3;j++)
    {

        g[i].push_back(j);

    }
    //printf("\n");
}
check(g);
}




other way for these type of vector (used in graph for adjacency list)

vector<int > g[n];// declaration

int dfs(vector<int> g[],int a,int n)

vector size declaration

syntax;
vector<vector<int>> A(dimension, vector<int>(dimension));