We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,011 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Declare 2D vector

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));
3
Contributors
6
Replies
1 Hour
Discussion Span
4 Years Ago
Last Updated
7
Views
Question
Answered
Jennifer84
Posting Pro
564 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
Skill Endorsements: 1
#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.

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Skill Endorsements: 6

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

Jennifer84
Posting Pro
564 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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 =)

Alex Edwards
Posting Shark
972 posts since Jun 2008
Reputation Points: 392
Solved Threads: 109
Skill Endorsements: 1

Thank you very much : )

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

Have fun =)

Jennifer84
Posting Pro
564 posts since Feb 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 4 Years Ago by Alex Edwards and twomers

>> 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.

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Skill Endorsements: 6

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0745 seconds using 2.68MB