Another example: boost::ref ...
#include <algorithm> #include <iostream> #include <vector> #include <iterator> #include <boost/ref.hpp> int main() { int n[] = { 3, 4, 1, 7, 10 }; std::vector<int> v(n, n + 5); // print the original sequence std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; // fill another vector with references to the data in the original std::vector<boost::reference_wrapper<int> > vp; std::transform(v.begin(), v.end(), std::back_inserter(vp), &boost::ref<int>); // sort the references std::sort(vp.begin(), vp.end()); // print the filtered version. std::copy(vp.begin(), vp.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; // print the original sequence again to show it hasnt changed std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); std::cout << std::endl; }
source share