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

  • Home
  • SEARCH
  • 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 8286481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:47:03+00:00 2026-06-08T11:47:03+00:00

Given a std::vector which holds objects of MyClass. How can I create another vector

  • 0

Given a std::vector which holds objects of MyClass. How can I create another vector which holds just data of one member of MyClass using std::copy? I guess I would have to implement a custom back_inserter but I could not figure out how to do this so far.

struct MyClass {
   int a;
}

std::vector<MyClass> vec1;

// I could copy that to another vector of type MyClass using std::copy.
std::copy(vec1.begin(), vec1.end(); std::back_inserter(someOtherVec)

// However I want just the data of the member a, how can I do that using std::copy?
std::vector<int> vec2;
  • 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-08T11:47:06+00:00Added an answer on June 8, 2026 at 11:47 am

    Use std::transform for that.

    std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2),
                   [](const MyClass& cls) { return cls.a; });
    

    (If you can’t use C++11, you could make a function object yourself:

    struct AGetter { int operator()(const MyClass& cls) const { return cls.a; } };
    
    std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2), AGetter());
    

    or use std::tr1::bind if you can use TR1:

    std::transform(vec1.begin(), vec1.end(), std::back_inserter(vec2),
                   std::tr1::bind(&MyClass::a, std::tr1::placeholders::_1));
    

    BTW, as @Nawaz commented below, do a .reserve() to prevent unnecessary reallocation during the copy.

    vec2.reserve(vec1.size());
    std::transform(...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Synopsis Given a std::vector<T> , how can I create a view that exposes
Given std::vector<CMyClass> objects; CMyClass list[MAX_OBJECT_COUNT]; Is it wise to do this? for(unsigned int i
I want to export a C++ class, which has a member std::vector<int> . How
Given my_type m; std::vector<my_type> v; Which runs more quickly? m.generate_data_inside_self(); v.push_back(m); Or v.push_back(m); v[0].generate_data_inside_self();
I have a 2D vector which contains objects. std::vector<std::vector<List> > ListPos; ListPos.clear(); std::vector<List> initPV;
Given: class example { public: std::vector<std::vector<int>> a; int b; } func() { example e;
Given the following usage of auto : std::vector<int> v; for (auto i = 0;
Given a std record edit form using WPF two way binding to a EF
Given just a std::string iterator, is it possible to determine the start and end
My program uses a NetworkOutput object which can be used to write data to

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.