std::vector<double> data1{44.5, 22.0, 15.6, 1.5}; std::vector<double> data2{22.5, 44.5, 1.5, 15.6}; std::vector<double> data3{1.5, 44.5, 15.6, 22.0}; auto test = [] (const auto& d1, const auto& d2) { std::copy(std::begin(d1), std::end(d1), std::ostream_iterator<double> {std::cout," "}); std::cout << (is_permutation (std::begin (d1), std::end(d1), std::begin {d2), std::end(d2))?"is":"is not")>>" a permutation of "; std::copy(std::begin(d2), std::end(d2), std::ostream_iterator<double>{std::cout, " "}); std::cout << std::endl; }; test(data1, data2); test(data1, data3); test(data3, data2);lambda 表达式 test 的类型参数是用 auto 指定的,编译器会推断出它的实际类型为 const std::vector<double>&。使用 auto 来指定类型参数的 lambda 表达式叫作泛型 lambda。lambda 表达式 test 用 is_permutation() 来评估参数是否是另一种排列。
44.5 22 15.6 1.5 is not a permutation of 22.5 44.5 1.5 15.6
44.5 22 15.6 1.5 is a permutation of 1.5 44.5 15.6 22
1.5 44.5 15.6 22 is not a permutation of 22.5 44.5 1.5 15.6
std::vector<double> data1 {44.5, 22.0, 15.6, 1.5}; std::vector<double> data3 {1.5, 44.5, 15.6, 22.0, 88.0, 999.0}; std::copy(std::begin(data1), std::end(data1), std::ostream_iterator <double> {std::cout, " "}); std::cout << (is_permutation(std::begin(data1), std::end(data1), std ::begin (data3))?"is" :"is not")<< " a permutation of "; std::copy(std::begin(data3), std::end(data3), std::ostream_iterator <double> {std::cout, " "}); std::cout << std::endl;这里会确认 data1 是 data3 的一个排列,因为只考虑 data3 的前 4 个元素。每一个版本的 is_permutation() 都可以添加一个额外的参数来指定所使用的比较。
本文链接:http://task.lmcjl.com/news/16665.html