tags:

views:

234

answers:

6

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++.

+1  A: 

If you can use it, probably the best idea is to use a function in the Standard Template Library.

For example, you might want to check out for_each or transform, which basically do just that.

Edan Maor
+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
+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
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
+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
Again with the not-yet-supported answer! :P
GMan
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
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
@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
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