Say there is a list of integers [1,2,3,4,5] and a map function that multiplies each element with 10 and returns modified list as [10,20,30,40,50] , with out modifying the original list. How this can be done efficiently in c++.
+3
A:
Here's an example:
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int multiply(int);
int main() {
vector<int> source;
for(int i = 1; i <= 5; i++) {
source.push_back(i);
}
vector<int> result;
result.resize(source.size());
transform(source.begin(), source.end(), result.begin(), multiply);
for(vector<int>::iterator it = result.begin(); it != result.end(); ++it) {
cout << *it << endl;
}
}
int multiply(int value) {
return value * 10;
}
Jason
2010-01-08 17:57:56
+1
A:
I only post this to illustrate using a functor in transform rather than a global function:
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
struct MulBy : public std::unary_function<int, int>
{
MulBy(int v) : v_(v) {}
int operator()(int lhs) const
{
return lhs * v_;
}
private:
int v_;
};
int main()
{
int incoming[5] = {1, 2, 3, 4, 5};
int result[5] = {0, 0, 0, 0, 0};
transform(&incoming[0], &incoming[5], &result[0], MulBy(10));
copy(&result[0], &result[5], ostream_iterator<int>(cout, " "));
return 0;
}
John Dibling
2010-01-08 18:07:26
A:
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
struct MulBy : public std::unary_function<int, int>
{
MulBy(int v) : v_(v) {}
int operator()(int lhs) const
{
return lhs * v_;
}
private:
int v_;
};
int main()
{
vector<int> ListOfNumber;
ListOfNumber.push_back(1);
ListOfNumber.push_back(2);
ListOfNumber.push_back(3);
ListOfNumber.push_back(4);
ListOfNumber.push_back(5);
vector<int> ListResult;
ListResult.resize(ListOfNumber.size());
//Produces a new list
transform(ListOfNumber.begin(),ListOfNumber.end(),ListResult.begin(),MulBy(10));
copy(ListOfNumber.begin(),ListOfNumber.end(),ostream_iterator<int>(cout,"\t"));
//Modifies the original list
transform(ListOfNumber.begin(),ListOfNumber.end(),ListOfNumber.begin(),MulBy(10));
copy(ListResult.begin(),ListResult.end(),ostream_iterator<int>(cout,"\t"));
cin.get();
}
yesraaj
2010-01-08 18:53:51
+3
A:
How about the following:
std::vector<int> src;
std::vector<int> dst;
std::transform(src.begin(),src.end(),
std::back_inserter(dst),
[](const int& v) -> int { return v * 10});
dman
2010-01-09 01:45:32
Again with the not-yet-supported answer! :P
GMan
2010-01-09 01:56:10
I think its a perfectly valid answer :> Also doesn't it look so much more elegant and simple when compared to some of the other answers... At some point people are going to have to accept this stuff as being real and valid.
dman
2010-01-09 02:01:41
If the question had been tagged C++Ox then it would have been a valid answer. But we are using standard C++ and most peoples compilers will not support this syntax.
Martin York
2010-01-09 04:06:58
@Martin: I think its a perfectly valid answer :> Also doesn't it look so much more elegant and simple when compared to some of the other answers... At some point people are going to have to accept this stuff as being real and valid.
dman
2010-01-14 21:05:04
A:
Along the lines of @darids answer, but current C++
std::vector<int> src;
std::vector<int> dst;
std::transform(src.begin(), src.end(),
std::back_inserter(dst),
std::bind1st(std::multiplies<int>(), 10));
spong
2010-01-09 03:08:06