Question

I have a map of the form map<double, tuple<double, double>> m1. Can I copy this to a map of the form map<double, double> m2 such that the keys are the same, and the value in m2 is get<0>(m1->second) without using a loop? Thanks!

Was it helpful?

Solution

Would something like this work for you?

vector<pair<double, double>> v(m1.size());
auto lambda = [](pair<double, tuple<double, double>> p){ return make_pair(p.first, get<0>(p.second)); };
transform(m1.begin(), m1.end(), v.begin(), lambda);
map<double, double> m2(v.begin(), v.end());

Note that we haven't really avoided a loop; we've just made std::transform do the looping for us. If even indirect looping isn't allowed, what you probably want is a transforming iterator. See http://www.boost.org/doc/libs/1_36_0/libs/iterator/doc/transform_iterator.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top