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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:17:10+00:00 2026-06-16T05:17:10+00:00

vector<vector<int>> sort_a; vector<int> v2; vector<int> v3; for (int i=0; i<4; ++i) { v2.push_back(i); for

  • 0
vector<vector<int>> sort_a;
vector<int> v2;
vector<int> v3;

for (int i=0; i<4; ++i) {
v2.push_back(i);

  for (int j=0; j<4; ++j) {
  v3.push_back(j);
  sort_a.push_back(v2);
  sort_a.push_back(v3);
  }

}

Vector sort_a should be a 4×4 array, instead the output is 31×1 with lots of empty elements, how do i insert elements in a multidimensional vector ?

  • 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-06-16T05:17:11+00:00Added an answer on June 16, 2026 at 5:17 am

    Don’t think of it as a multidimentional vector, think of it as a vector of vectors.

    int n = 4;
    std::vector<std::vector<int>> vec(n, std::vector<int>(n));
    
    // looping through outer vector vec
    for (int i = 0; i < n; i++) {
      // looping through inner vector vec[i]
      for (int j = 0; j < n; j++) {
        (vec[i])[j] = i*n + j;
      }
    }
    

    I included parentheses in (vec[i])[j] just for understanding.

    Edit:

    If you want to fill your vector via push_back, you can create a temporary vector in the inner loop, fill it, and then push_back it to your vector:

    for (int i = 0; i < n; i++) {
      std::vector<int> temp_vec;
    
      for (int j = 0; j < n; j++) {
        temp_vec.push_back(j);
      }
    
      vec.push_back(temp_vec);
    }
    

    However, push_back calls result in slower code, since not only you need to reallocate your vector all the time, but also you have to create a temporary and copy it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

vector<int> l; for(int i=0;i<10;i++){ l.push_back(i); } I want the vector to only be able
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
Because of my algorithm i have to insert some empty(NULL) nodes into my vector,some
I read a integer binary file to int vector. When I use the Sort
Suppose we'd like to remove duplicate values from a vector of int s. The
I have a function that creates a 2D vector void generate(int n) { vector<
vector<int>& mergesort(vector<int> &a) { if (a.size() == 1) return a; int middle = a.size()
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,
header file: private: vector<int*>* nums; public slots: void buttonClicked(); cpp file: NewWindow(){ int one
Consider this: std::vector<int*> v(1, 0); This compiles fine with VC++10 (no warnings even at

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.