Creating a new vector using a transform

Nothing fancy. Just a two line solution which I like:

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
    std::vector<int>                  a{1,2,3,4,15};
    std::vector<std::pair<bool, int>> b;

    auto it = a.begin();
    std::generate_n(std::back_inserter(b), a.size(), [&it]() { return std::make_pair(false, *it++); });

    for(auto c : b) {
        std::cout << std::boolalpha << c.first << " " << c.second << "\n";
    }
}

Demo


1. You could make a functor and std::for_each:

struct F {
    F(std::vector<std::pair<bool,int> > &b) : m_b(b){
    }

    void operator()(int x) {
        m_b.push_back(std::make_pair(false, x));
    }

    std::vector<std::pair<bool,int> > &m_b;
};

std::for_each(a.begin(), a.end(), F(b));

Though this may prove to be more trouble than it's worth. But at least it would be reusable :).

Maybe there is something that could be done with boost::bind.

2. EDIT: I was thinking you might be able to use bind with a back inserter and transform. something like this:

std::transform(a.begin(), a.end(), std::back_inserter(b), boost::bind(std::make_pair<bool, int>, false, _1));

I tried this with std::bind1st, i thought it should have worked, but i could only get it to succeed with boost::bind. I'll keep trying...

3. EDIT: here's a non-boost solution:

std::transform(a.begin(), a.end(), std::back_inserter(b), std::bind1st(std::ptr_fun(std::make_pair<bool, int>), false));

4. EDIT: here's a C++11 solution (which is my current favorite):

std::for_each(begin(a), end(a), [&b](int v) {
    b.emplace_back(false, v);
});

or even simpler:

for(int v : a) {
    b.emplace_back(false, v);
}

Tags:

C++

Stl