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 3604720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:02:01+00:00 2026-05-18T21:02:01+00:00

I’ve a 2d array, say A[2][3]={{1,2,3},{4,5,6}}; and I want to push it into a

  • 0

I’ve a 2d array, say A[2][3]={{1,2,3},{4,5,6}}; and I want to push it into a 2D vector(vector of vectors). I know you can use two for loops to push the elements one by on on to the first vector and then push that into the another vector which makes it 2d vector but I was wondering if there is any way in C++ to do this in a single loop. For example I want to do something like this:

myvector.pushback(A[1]+3); // where 3 is the size or number of columns in the array.

I understand this is not a correct code but I put this just for understanding purpose. Thanks

  • 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-18T21:02:02+00:00Added an answer on May 18, 2026 at 9:02 pm

    The new C++0x standard defines initializer_lists which allows you to:

    vector<vector<int>> myvector = {{1,2,3},{4,5,6}};
    

    gcc 4.3+ and some other compilers have partial C++0x support.
    for gcc 4.3+ you could enable c++0x support by adding the flag -std=c++0x

    Its not the best way to have your static data represented like that. However, if your compiler vendor supports C++ tr1 then you could do:

    #include <tr1/array>  // or #include <array>
    ...
    
    typedef vector<vector<int> > vector2d;
    vector2d myvector;
    
    // initialize the vectors
    myvector.push_back(vector<int>());
    myvector.push_back(vector<int>());
    
    typedef std::array<std::array<int, 3>, 2> array2d;
    array2d array = {{1,2,3},{4,5,6}};
    array2d::const_iterator ai = array.begin(), ae = array.end();
    for (vector2d::iterator i = myvector.begin(), e = myvector.end()
        ; i != e && ai != ae
        ; i++, a++)
    {
        // reserve vector space
        i->reserve(array.size());
    
        // copy array content to vector
        std::copy(ai.begin(), ai->end(), i->begin());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.