Thread: Help with vector usage

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    Help with vector usage

    I'd like to define a vector that contains 5 vectors, and each of the 5 vectors should contain 5 ints. But it cannot be done by using the following statement:

    vector< vector<int> > vec(5,5); // error!

    What can I do to define such a vector?

    Thank you in advance.
    Last edited by Antigloss; 06-21-2005 at 07:32 PM.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Interesting. I myself do not work with preinitialized vector within a vector. You could always manually construct the vectors.

    Kuphryn

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    vector<vector<int> > vec(1,vector<int>(5,5));
    Last edited by Stoned_Coder; 06-21-2005 at 08:56 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Thanks Stoned_Coder ^_^
    It works

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Stoned_Coder
    Code:
    vector<vector<int> > vec(1,vector<int>(5,5));
    that creates a vector that contains one vector that holds 5 ints with value 5

    here's the answer
    Code:
    vector<vector<int> > vec(5,vector<int>(5));
    almost there

Popular pages Recent additions subscribe to a feed