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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:24:48+00:00 2026-06-02T16:24:48+00:00

A std::vector can be constructed using a C-array like so: std::vector<int> vec(ary, ary +

  • 0

A std::vector can be constructed using a C-array like so: std::vector<int> vec(ary, ary + len). What is the proper way to construct a std::vector<std::vector<int> >?

I’ve been brute-forcing the issue by manually copying each element into the vector, clearly this is not the intent, but it works.

int map[25][18] = { /*...DATA GOES HERE...*/ }
std::vector<std::vector<int> > m(18, std::vector<int>(25, 0));
for(int y = 0; y < 18; ++y) {
    for(int x = 0; x < 25; ++x) {
        m[y][x] = map[y][x];
    }
}
  • 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-02T16:24:50+00:00Added an answer on June 2, 2026 at 4:24 pm
    int map[18][25] = { /*...DATA GOES HERE...*/ };
    std::vector<std::vector<int> > m;
    m.reserve(18);
    for (std::size_t i = 0; i != 18; ++i)
        m.push_back(std::vector<int>(map[i], map[i] + 25));
    

    In C++11, you can optionally replace the last line with the following to remove a little noise:

        m.emplace_back(map[i], map[i] + 25);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
std::vector<std::pair<std::string > > can be used to store a list of a pair of
I want a std::vector to contain some functions, and that more functions can be
std::vector<Foo> vec; Foo foo(...); assert(vec.size() == 0); vec.reserve(100); // I've reserved 100 elems vec[50]
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
Is there a way to make a non-resizeable vector/array of non-reassignable but mutable members?
#include <iostream> #include <string> #include <fstream> #include <vector> using namespace std; class Dict {
I am trying to initialise an std::vector<std::unique_ptr<std::string>> in a way that is equivalent to
I am using vector in my code std::vector<CEventLogInfo > class CEventLogInfo { // date

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.