I think this could be a simple question. I have declared a 2D vector like below.
The thing is that by default all elements is set to 0.
What I want to do is to declare all of these elements to: -1 how this is possible without doing that with a loop ?

std::vector<std::vector<int> > vec2(100, std::vector<int>(50));

Vectors have an overloaded Constructor that allows two arguments...

I do believe, in that scenario, the first argument is the initial size and the second is the default value for all values in the vector.

std::vector<std::vector<int> > vec2(100, std::vector<int>(50, -1));

Source

#include <iostream>
#include <vector> 


int main( void ) {
  // 4x5 (4 in x, 5 in y) array, containing -1
  std::vector< std::vector<int> > vvint( 5, std::vector<int>( 4, -1 ) );

  for( int j=0; j<vvint.size(); j++ ) {
    for ( int i=0; i<vvint.at(0).size(); i++ ) {
      std::cout<< vvint[j][i] << " ";
    }
    std::cout<< "\n";
  }

  return 0;
}

Edit: Beaten to it. Forgot to post.

I didn´t know that there was 2 arguments. Good to know now :)
So if I understand correct for a simpler example below, all 36 elements
is declared to: -1
Thank you !

std::vector<std::vector<int> > vec2(5, std::vector<int>(5, -1));

Vectors have an overloaded Constructor that allows two arguments...

I do believe, in that scenario, the first argument is the initial size and the second is the default value for all values in the vector.

std::vector<std::vector<int> > vec2(100, std::vector<int>(50, -1));

Source

I didn´t know that there was 2 arguments. Good to know now :)
So if I understand correct for a simpler example below, all 36 elements
is declared to: -1
Thank you !

std::vector<std::vector<int> > vec2(5, std::vector<int>(5, -1));

I'm pretty sure you meant 25 (5*5), but I believe you get the idea.

Have fun =)

Thank you very much : )

I'm pretty sure you meant 25 (5*5), but I believe you get the idea.

Have fun =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.