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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:27:51+00:00 2026-06-01T10:27:51+00:00

I have a Visual Studio 2008 C++ project where I would like to copy

  • 0

I have a Visual Studio 2008 C++ project where I would like to copy one struct element of a vector of that struct type to a new vector. For example:

struct Foo {
    int a;
    long b;
};

std::vector< Foo > v1;
std::vector< long > v2;

for( std::vector< Foo >::const_iterator it = v1.begin(); it != v1.end(); ++it )
{
    v2.push_back( it->b );
}

Is there a better/more elegant way than this?

Thanks,
PaulH

  • 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-01T10:27:52+00:00Added an answer on June 1, 2026 at 10:27 am

    In Visual C++ 2008, no, this is about as “elegant” as it gets. The Standard Library provides algorithms that can be used to manipulate containers, but in most scenarios–especially in simple use cases like this one–they are far too cumbersome to use.

    C++11 adds lambda expressions to C++. Visual C++ 2010 and recent versions of other C++ compilers support this C++11 feature. With lambda expressions, you can easily use the transform algorithm for your task:

    std::transform(v1.begin(), v1.end(), std::back_inserter(v2),
                   [](Foo const& x) { return x.b; });
    

    Without lambda expressions, you’d have to define a function to extract the b element from the struct:

    long get_b(Foo const& x) { return x.b; }
    

    You could then use this function with the transform algorithm:

    std::transform(v1.begin(), v1.end(), std::back_inserter(v2), get_b);
    

    However, for simple use cases like this, this can quickly lead to unwieldy code as it is difficult to neatly keep all of the related functions together.

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

Sidebar

Related Questions

I have a Visual Studio 2008 C++ project where I would like to heap
I have a simple unmanaged c++ project in Visual Studio 2008, and would like
I have a website project in Visual Studio 2008. I would like to build
I have a Visual Studio 2008 C++ project that has support for using multiple
I have a Visual Studio 2008 C++ project that outputs a static library and
I have a .DLL that i include in my Visual Studio 2008 project. The
I have a Visual Studio 2008 C# .NET 3.5 project that I am implementing
I have a visual studio 2008 solution that includes an asp.net-hosted remoting project in
I have a Visual Studio 2008 C++ project for Windows 7 where I would
I have a Visual Studio 2008 C++ Windows Mobile 6.5 project where I would

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.