Sorry for a little bit beginner question. There are vector and vector of pairs
typedef std::vector <int> TItems;
typedef std::vector < std::pair <int, int> > TPairs;
Is there any way to transform all first items in pair to another vector in one step
int main ()
{
TItems items;
TPairs pairs;
pairs.push_back (std::make_pair(1,3));
pairs.push_back (std::make_pair(5,7));
std::transform( items.begin(), items.end(), items.begin(), comp ( &pairs ) );
return 0;
}
How to design a functor?
class comp
{
private:
TPairs *pairs;
public:
comp ( TPairs *pairs_ ) : pairs ( pairs_) { }
unsigned int operator () ( const unsigned int index ) const
{
return (*pairs)[index].second != pairs->end(); //Bad idea
}
};
Maybe there is some more user friendly method without lambda expressions and loops. Thanks for your help.
First of all, you should use a
back_inserteras the third argument totransformso that the transformed values are pushed to the back of the vector.Second, you need some sort of functor which takes a pair of ints and returns the first one. This should do:
Now, to put the pieces together:
After this code,
itemscontains 1 and 5.