-2

So I dont understand how to use inserters in this situation. I know what are inserters, I know about std::front_inserter and std::back_inserter and std::inserter but I am confused about this problem which I will present now. I need to make function, which will transform elemets of vector and put them in deque(or vector nevermind, its "generic" function anyway). That function has 5 parameters, which one of them is another function(which can have only one parameter, it is not specified what type(i mean it can be reference,iterator,pointer...... whatever)).

If my vector is:

std::vector<int> v={1,2,3,4,5};

I need to make some modification, with lambda function, which will make my deque have elements like this:

25 16 9 4 1

So you see that first element of deque is last element of vector ^2 (you can see what I want to do). So my question is: How can the problem be done using inserters? I mean should I somehow put inserter in lambda fucntion? Maybe lambda should be like this:

[](int x) { 
x=x*x;
std::front_inserter(q);
}

I was thinking about this but then I dont understand how will this lambda work when I send it as parameter of this "big" function? How it will know what is q inside big function?

I hope you understand what I want to do.

Here is example. So I have to make some function, and this is prototype(lets say it is void):

typename<template Type1, template Type2>
void Fun(Type1 p1,Type1 p2,Type2 p3,Type2 p4,void (*f)(std::remove_reference<decltype(*p1)>::type) );

Lets say that I have the following code in main:

int main() {
std::vector<int> v={1,2,3,4,5};
std::deque<int> d(5);
Fun(v.begin(),v.end(),d.begin(),d.end(), /* some lambda function */);
pino123
  • 13
  • 6
  • "*I hope you understand what I want to do.*" No, I don't. What are the 5 parameters of your function for? – melpomene Apr 21 '19 at 16:14
  • You can see from the call of Fun ... – pino123 Apr 21 '19 at 16:19
  • Yes, that call makes no sense. Please describe the exact terms of the problem that you're trying to solve with this function. – melpomene Apr 21 '19 at 16:22
  • I mean instead of creating function, I can use some already existing funciton. That make sense now? And then what function should I use? std::front_inserter ? – pino123 Apr 21 '19 at 16:27
  • As far as I can tell, it is impossible to accomplish that with a function that has that type. And with a range given for output, inserters are pointless. – molbdnilo Apr 21 '19 at 16:27
  • Output deque can be with no dimension. – pino123 Apr 21 '19 at 16:30
  • It looks like you're trying to build std::transform and you are new to generic programming. To answer your question, the functor's argument must be the same value type as the source container. https://en.cppreference.com/w/cpp/algorithm/transform – ppetraki Apr 21 '19 at 16:34

1 Answers1

2

If you're only interested in the transformation, not in implementing a function with that type,

std::deque<int> d;
std::transform(v.begin(), v.end(), std::front_inserter(d), [](int x){return x * x;});

or

std::deque<int> d;
std::transform(v.rbegin(), v.rend(), std::back_inserter(d), [](int x){return x * x;});
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • 1
    Couldn't you just do `std::for_each(v.rbegin(), v.rend(), [&](int x) { d.push_back(x*x); });`? – melpomene Apr 21 '19 at 16:36
  • @melpomene That depends on what level of the spheres of functional programming you're comfortable in. – molbdnilo Apr 21 '19 at 16:37
  • He could, but I point that I want to use inserters ;) Thanks I think I can use this for solving my problem. So I can normally isntead of d.begin() use std::front_inserter(d) ? – pino123 Apr 21 '19 at 16:39
  • @pino123 You would use regular iterators for overwriting, inserters for inserting. – molbdnilo Apr 21 '19 at 17:13