Search code examples
c++algorithmoopstlstl-algorithm

Generate container from class memebers


I am wondering if there is an algorithm in the std library which is able to convert a container containing a class type into another container holding a member of that class type.

So what I am basically looking for is an STL alternative for a function like copy_acces in the code below:

template<class InputIterator, class OutputIterator, class Accessor>
OutputIterator copy_acces(InputIterator first, InputIterator last,
                          OutputIterator result, Accessor acc) 
{
    while (first!=last) {
        *result = acc(*first);
        ++result;
        ++first;
    }

    return result;
}

Which can be used like this:

using namespace std;

struct A {
    int i;    
};

int main()
{
    vector<A> vecA = {{0}, {1}, {2}};
    vector<int> vecInt;

    copy_acces(vecA.begin(), vecA.end(), 
               back_insert_iterator< vector<int> >(vecInt), 
               [](A a){ return a.i; });

    for(int i : vecInt) {
        cout << i << endl;   
    }
}

Solution

  • For something cheap to default construct like an int I would use std::transform like this:

    std::vector<int> vecInt(vecA.size());
    std::transform(vecA.begin(), vecA.end(), vecInt.begin(), [](const A& a){ return a.i; });
    

    Live demo.

    For something that is not default constructible or expensive to default construct you can't or won't want to construct a vector of the right size first. You can still use std::transform with a back_inserter but I start to think that a simple ranged-based for loop is easier to read:

    std::vector<ExpensiveToConstruct> vecB;
    vecB.reserve(vecA.size());
    
    for (const auto& a : vecA) {
        vecB.push_back(a.e);
    }
    

    Live demo.