I would like to apply an algorithm on a projection of a container, but then use that information to retrieve information from the original container. In the contrived example below, I would like to use iter
to find the related key-value pair in my_vec
. I know there are simpler ways to solve this example in particular, but I would like to apply this to more complex projections.
#include <iostream>
#include <range/v3/algorithm/min_element.hpp>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/join.hpp>
#include <range/v3/view/map.hpp>
int main() {
std::unordered_map<std::string, std::vector<int>> my_map{
{"1", {500, 30}}, {"2", {20, 60}}, {"3", {14, 10}}, {"4", {5, 4}}, {"5", {30, 56}}};
auto my_view = my_map | ranges::views::values | ranges::views::join | ranges::to_vector;
auto iter = ranges::min_element(my_view);
for(const auto& val : my_view) { std::cout << val << ","; }
std::cout << std::endl << *iter << "," << std::distance(my_view.begin(), iter) << std::endl;
}
Outputs:
5,4,20,60,30,56,14,10,500,30,
4,1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…