Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;   
    }
}
like image 709
exilit Avatar asked Feb 11 '26 18:02

exilit


1 Answers

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.

like image 85
Chris Drew Avatar answered Feb 13 '26 08:02

Chris Drew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!