example3_using_with_algorithms.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <functional>
#include <algorithm>
#include <vector>
#include <iostream>

int main() {
    std::vector<int> numbers = {5, 2, 8, 1, 9};
    std::vector<std::reference_wrapper<int>> refs(numbers.begin(), numbers.end());
    
    std::sort(refs.begin(), refs.end(), 
              [](const int& a, const int& b) { return a > b; });
    
    for (const auto& ref : refs) {
        std::cout << ref << " ";
    }
    std::cout << std::endl;
    
    return 0;
}
Back to std_reference_wrapper