1,105,399 Community Members

Declare 2D vector

Member Avatar
Jennifer84
Posting Pro
563 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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));
Member Avatar
Alex Edwards
Posting Shark
971 posts since Jun 2008
Reputation Points: 321 [?]
Q&As Helped to Solve: 109 [?]
Skill Endorsements: 2 [?]
 
0
 

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

Member Avatar
twomers
Posting Virtuoso
1,873 posts since May 2007
Reputation Points: 408 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 12 [?]
 
0
 
#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.

Member Avatar
Jennifer84
Posting Pro
563 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Alex Edwards
Posting Shark
971 posts since Jun 2008
Reputation Points: 321 [?]
Q&As Helped to Solve: 109 [?]
Skill Endorsements: 2 [?]
 
0
 

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 =)

Member Avatar
Jennifer84
Posting Pro
563 posts since Feb 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you very much : )

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

Have fun =)

Question Answered as of 5 Years Ago by Alex Edwards and twomers
Member Avatar
twomers
Posting Virtuoso
1,873 posts since May 2007
Reputation Points: 408 [?]
Q&As Helped to Solve: 68 [?]
Skill Endorsements: 12 [?]
 
0
 

>> I didn´t know that there was 2 arguments
There's one way to find out, use a reference. www.cplusplus.com is a good one, IMO. Here's vector's constructor: http://www.cplusplus.com/reference/stl/vector/vector.html and at the bottom there are examples.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article