Question: I have a class/structure as follows:

```
struct MyStruct {
  std::string member_one;
  std::string member_two;
};

```

I create a vector of `MyStruct`, `std::vector<MyStruct>` of some length `N` with the members set to custom values:

```
std::vector<MyStruct> my_struct_vect(10);
// initialize class instances

```

Now I want to extract the first member `memberOne` into a new vector. I can do so as follows:

```
std::vector<std::string> member_one_vect(my_struct_vect.size());
for (size_t i = 0; i < my_struct_vect.size(); ++i) {
  member_one_vect[i] = my_struct_vect[i].member_one;
}

```

My question is, is there a quicker/more elegant/cleaner way of doing this without having to write the custom loop every time? In Python for example, I could do this quite easily with comprehensions. I'm not expecting something similar in C++, but am wondering if there is some way to simplify this nonetheless.

Update
------

Thanks to the great responses on using `std::transform` and `boost::adaptors::transformed`. These are very useful, but in order to be compact it's worth noting they rely on lambda functions which were introduced in C++11 (it's possible to use them without, but this requires defining a separate helper function).

So for bonus points, are there any ways to do this in a compact way in C++03?

Answer: 
You could use [`std::transform`](http://en.cppreference.com/w/cpp/algorithm/transform):

```
#include <algorithm> // Necessary for std::transform()

// ...

std::vector<std::string> member_one_vect(my_struct_vect.size());

std::transform(
    my_struct_vect.begin(), my_struct_vect.end(), member_one_vect.begin(),
    [] (MyStruct const& ms)
{
    return ms.member_one;
});

```

Here is how the full code would look like:

```
#include <vector>
#include <string>
#include <algorithm>

struct MyStruct {
  std::string member_one;
  std::string member_two;
};

int main()
{
    std::vector<MyStruct> my_struct_vect(10);

    // Initialize my_struct_vect...

    std::vector<std::string> member_one_vect(my_struct_vect.size());

    std::transform(
        my_struct_vect.begin(), my_struct_vect.end(), member_one_vect.begin(),
        [] (MyStruct const& ms)
    {
        return ms.member_one;
    });

    // Do something with member_one_vect...
}

```

Here is a [**live example**](http://coliru.stacked-crooked.com/view?id=2292d2200fdbff81ee0ae778b2ee1482-5fc7d541d7d8add59deca2ec8078dfbb).
You can use algorithms.

```
std::vector<std::string> member_one_vect(my_struct_vect.size());
std::transform(my_struct_vect.begin(), my_struct_vect.end(),
member_one_vect.begin(),
[](const MyStruct& m) { return m.member_one; });

```

Also, you can use `boost::adaptors::transformed`.

```
#include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/copy.hpp>

std::vector<std::string> member_one_vect(my_struct_vect.size());
std::function<std::string(const MyStruct&)> transform =
[] (const MyStruct& m) { return m.member_one; };
boost::copy(my_struct_vect | boost::adaptors::transformed(transform),
member_one_vect.begin());

```