Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 738717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:52:06+00:00 2026-05-14T07:52:06+00:00

I know vectors can be constructed to a predefined size vector<int> foo(4); But is

  • 0

I know vectors can be constructed to a predefined size

vector<int> foo(4);

But is there a way to specify the dimensions of nested vectors?

vector< vector<int> > bar(4);

Lets say I wanted a vector of size 4 containing vector’s of size 4… like a 4×4 multidimensional array of ints?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:52:07+00:00Added an answer on May 14, 2026 at 7:52 am

    The second argument to that constructor is the value to initialize with. Right now you’re getting 4 default-constructed vectors. To clarify with a simpler 1D example:

    // 4 ints initialized to 0
    vector<int> v1(4);
    
    // *exactly* the same as above, this is what the compiler ends up generating
    vector<int> v2(4, 0); 
    
    // 4 ints initialized to 10
    vector<int> v3(4, 10); 
    

    So you want:

    vector< vector<int> > bar(4, vector<int>(4));
    //              this many ^   of these ^
    

    This creates a vector of vectors of ints, initialized to contain 4 vectors that are initialized to contain 4 ints, initialized to 0. (You could specify a default value for the int to, if desired.)

    A mouth-full, but not too hard. 🙂


    For a pair:

    typedef std::pair<int, int> pair_type; // be liberal in your use of typedef
    typedef std::vector<pair_type> inner_vec;
    typedef std::vector<inner_vec> outer_vec;
    
    outer_vec v(5, inner_vec(5, pair_type(1, 1)); // 5x5 of pairs equal to (1, 1)
    //             this many ^ of these ^
    //this many ^      of these ^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Know there's a Vector class in java, but it seems to be just
Is there a way to make a non-resizeable vector/array of non-reassignable but mutable members?
I know that std::vectors do not 'free' the memory when you call vector.clear(), but
I like vectors a lot. They're nifty and fast. But I know this thing
Does anybody know why vector<int> test(10); int a=0; for_each(test.begin(),test.end(),(_1+=var(a),++var(a))); for_each(test.begin(),test.end(),(cout << _1 << ));
I think I've declared a Vector with an object correctly. But, I don't know
I know there are quite a few questions concerning access violations, but I have
I know coordinates of vectors A and B. How can I count first point
I'm new with vectors. I'm trying to add objects to a vector. But the
Do you know of a good library/way that I can use to solve an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.