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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:47:20+00:00 2026-06-03T00:47:20+00:00

I am trying to copy the vector data from sample to Y as below

  • 0

I am trying to copy the vector data from sample to Y as below

std::map<std::string, std::vector<double > >sample;
std::map<std::string, std::vector<double > >::iterator it1=sample.begin(), end1=sample.end();
std::vector<double> Y; 

and am using the following code:

 while (it1 != end1) {
  std::copy(it1->second.begin(), it1->second.end(), std::ostream_iterator<double>(std::cout, " "));
++it1;
}

It prints the output ok, however when I replace the above std::copy block with the below, I get a segfault.

 while (it1 != end1) {
std::copy(it1->second.begin(), it1->second.end(), Y.end());
++it1;
}

I just want to copy the contents of it1->second to Y. Why is it not working and how do I fix it?

  • 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-03T00:47:21+00:00Added an answer on June 3, 2026 at 12:47 am

    Obviously, you want to insert objects into your vector. However, std::copy() merely takes the iterators passed and writes to them. The iterators obtained by the begin() and end() iterators don’t do any insertion. What you want to use is something like this:

    std::copy(it1->second.begin(), it1->second.end(), std::back_inserter(Y));
    

    The std::back_inserter() function template is a factory function for an iterator using push_back() on its argument to append objects.

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

Sidebar

Related Questions

I'm trying to copy a QList in a std::vector this is my code: std::copy(_param_31.listJobs->list_USCOREjobs.begin(),
Greetings, I am trying to perform a copy from one vector (vec1) to another
I am trying to copy data from my MYSQL table to SQL Server using
Im trying to copy an array to a vector, however, when the data is
I am trying to remove an element from a vector using std::remove and std::erase.
I'm trying copy a single file from the Plugin directory inside of my Wordpress
I am trying to copy a header and a set of data to a
I am trying to copy a file from my remote server to my local.
I'm trying to write a class which contains several std::vectors as data members, and
Why am I getting runtime error while trying to do: std::vector<int> vi; std::generate_n(std::back_inserter(vi),10,rand); std::vector<int>

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.