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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:47:54+00:00 2026-05-22T22:47:54+00:00

I got a compiling error when using std::copy to convert a 1D vector to

  • 0

I got a compiling error when using std::copy to convert a 1D vector to a 2D vector.

int main() 
{  
    std::vector< std::vector<float> > v2D(10, std::vector<float>(10, 0.0)); 
    std::vector<float> v1D(100, 0.0);

    for (int i = 0; i < 100; ++i)
        v1D.push_back(i);

    for (unsigned int j = 0; j < 10; j++)
    {
        std::copy(v1D.begin() + j * 10, v1D.begin() + (j + 1) * 10, std::back_inserter(v2D[j].begin()));
    }
}

Would you please help solve this proplem? Thank you.

  • 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-22T22:47:54+00:00Added an answer on May 22, 2026 at 10:47 pm

    std::back_inserter takes a container, not an iterator. Change std::back_inserter(v2D[j].begin()) to std::back_inserter(v2D[j]). Note that this will be calling .push_back() on the std::vector<float> at v2D[j], so you probably also want to change std::vector< std::vector<float> > v2D(10, std::vector<float>(10, 0.0)); to std::vector< std::vector<float> > v2D(10);.

    Alternatively, you can change std::back_inserter(v2D[j].begin()) to v2D[j].begin(). This works because std::copy wants an output iterator, and std::vector<>::iterator behaves as one when there are a sufficient number of elements in the vector<> to overwrite. And this way, your current initialization of v2D is already ideal.


    EDIT: Someone else said this in a separate answer then deleted it, so I’ll say it on their behalf because it’s definitely noteworthy: because you initialize v1D with 100 elements, the [1..100] digits you then push_back are appended to the initial 100 elements (which all have your specified value of 0) rather than overwriting them. You should change std::vector<float> v1D(100, 0.0); to std::vector<float> v1D; to get the behavior you apparently want (or std::vector<float> v1D; v1D.reserve(100); if you’re really being pedantic about efficiency).

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

Sidebar

Related Questions

I got the undefined reference to `SetTextColor@8' error while compiling a c++ program using
I've got an error while using find() function. Here is the code: #include <iostream>
Has anyone out there got a good set of instructions for building/compiling Ruby from
I got this error today when trying to open a Visual Studio 2008 project
I have a vector of Student objects which I want to sort using #include
Compiling on Fedora 10. I am using qt for the first time. I started
I was compiling a C++ program in Cygwin using g++ and I had a
I'm cross-compiling an application, but linking blows up with an error that it cannot
I'm using ASP.NET with VB, and .NET version 3.5. In the project, I've got
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now

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.