vector<> adding new row?

I have a vector with 1x4
1: row size
4: coloumn size
How can I make it 2x4 dynamically?
then 3x4 then 4x4...
Last edited on
push_back() to each row to increase its size by one.
resize() each row to change its size by more than one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
    typedef std::vector<int> row ;
    std::vector<row> cols = { {0}, {1}, {2}, {3} } ;
    auto print = []( const std::vector<row>& v )
        {
            for( auto r : v )
                { for( auto i : r ) std::cout << i << ' ' ;  std::cout << '\n' ; } ;
        };
    print(cols) ;

    int i = 10 ; for( auto& r : cols ) r.push_back(++i) ;
    print(cols) ;

    i = 100 ; for( auto& r : cols ) r.push_back(++i) ;
    print(cols) ;

    for( auto& r : cols ) r.resize( 6U, 99 ) ;
    print(cols) ;
}

thanks for reply but ur code gave lots of syntax error i cant test it ? :\
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <vector>
#include <iostream>

typedef std::vector<int> row ;

void print( const std::vector<row>& v )
{
    for( std::size_t i=0 ; i < v.size() ; ++i )
    {
        for( std::size_t j = 0 ; j < v[i].size() ; ++j ) std::cout << v[i][j] << ' ' ;
        std::cout << '\n' ;
    }
}

int main()
{
    std::vector<row> cols = { {0}, {1}, {2}, {3} } ;
    print(cols) ;

    int n = 10 ;
    for( std::size_t i=0 ; i < cols.size() ; ++i ) cols[i].push_back(++n) ;
    print(cols) ;

    n = 100 ;
    for( std::size_t i=0 ; i < cols.size() ; ++i ) cols[i].push_back(++n) ;
    print(cols) ;

    for( std::size_t i=0 ; i < cols.size() ; ++i ) cols[i].resize( 6U, 99 ) ;
    print(cols) ;
}

std::vector<row> cols = { {0}, {1}, {2}, {3} } ;

`cols' must be initialized by constructor, not by `{...}' 
> std::vector<row> cols = { {0}, {1}, {2}, {3} } ;

Yeah, missed that. Replace with:
std::vector<row> cols( 4u, row(1U) ) ;
vectors increase their size automatically when you add elements to it. So in fact a vector with 1x4 is equivalent to a vector 4 and can be enlarged to a vector of max_size(). Vectors have no fixed number of elements. If you are not going to enlarge your vector you can, for example, for a vector 3x4 to write

std::vector<std::vector<int>> v( 3, 4 );
and then to set values to its elements as for example

v[0][2] = 10;
Last edited on
I defined my vector like in the following link (Line 5) and tried to add a row like you said (Line 6), but i still get error... what am i doing wrong? I just wanna add a row, column size is fixed. For example:
First: (defining the 2D vector)
* * * * 

(add row command)
After:
* * * *
* * * *


Thats all I want...

1
2
3
4
5
6
int m;
cin>>m;
//vector<vector<int> > dynamicArray(ROWS, vector<int>(COLUMNS)); 
// Link: http://www.codeproject.com/Articles/21909/Introduction-to-dynamic-two-dimensional-arrays-in
vector<vector<int> > tabu(1, vector<int>(m)); //(1xm)
std::vector<std::vector<int>> tabu(tabu.size()+1, 4 );
Last edited on
I do not understand your code

1
2
vector<vector<int> > tabu(1, vector<int>(m)); //(1xm)
std::vector<std::vector<int>> tabu(tabu.size()+1, 4 );


Why two vectors are defined with the same name? I understand the first statement if to rewrite it simply

std::vector<std::vector<int>> tabu( 1, m );

But I do not understand what you meant when you are defining the seconst vector with the same name as the first?! If you want that the vactor 1 x M will be a vector 4 x m then you should push_back three additional elements to it.

For example

1
2
3
4
5
6
std::vector<std::vector<int>> tabu( 1, m );

for ( int i = 0; i < 3; i++ )
{
   tabu.push_back( std::vector<int>( m ) );
}
ah i see ok. thanks a lot buddy :)
but one more question:

can defining part be as a global vector?
Because the "m" variable is a keyboard input. I have to define my vector after the input. right?

The outline is like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>

using namespace std;

//global variables
//function prototypes

int main()
{
int m;
cin>m;
std::vector<std::vector<int>> tabu( 1, m );
}

void tabuSearch()
{
for ( int i = 0; i < 3; i++ )
{
   tabu.push_back( std::vector<int>( m ) ); //=> tabu isnt declared...
}
}
Last edited on
Change your function the following way

1
2
3
4
5
6
7
8
void tabuSearch( std:;vector<std::vector<int>> &v )
{
   int m = v[0].size();
   for ( int i = 0; i < 3; i++ )
   {
      v.push_back( std::vector<int>( m ) );
   }
}


and then in main call it as tabuSearch( tabu );
thanks a lot buddy..
well. lets say i have:

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;
int **a; //Global Variable
int main()
{
   n=5;
   a = new int*[n];
   for ( int i = 0 ; i < n ; i++ ) a[i] = new int[n] ;
}


is there any realloc() method to increase the row size +1? (column size is fixed again)
I mean if the row size is 5 then i wanna make it 6.
I use

a[i] = (int *) realloc(a,sizeof(a)+1)*sizeof(int));

but i think something is wrong....
You should select either you use std::vector or you use dynamically allocated memory in heap.
I think that there is no any sense to answer your question because you do not know what you wont.
Topic archived. No new replies allowed.