巧用nth_element求容器前n%大小的那个数

 1 #include <algorithm>
 2 #include <vector>
 3 #include <iostream>
 4 #include <string>
 5 #include <numeric>
 6 std::string::size_type stringLengthSum(std::string::size_type sumSoFar, const std::string& s)
 7 {
 8     return sumSoFar += s.size();
 9 }
10 int main()
11 {
12     std::vector<int> v{1,2,3,4,5,6,7,8,9,10,11,12};
13     auto begin = v.begin();
14     auto end = v.end();
15     decltype(begin) goodPosition;
16     goodPosition = begin + 0.3 * v.size();
17     nth_element(begin, goodPosition, end);
18     std::cout << *goodPosition << std::endl;
19     return 0;
20 }
原文地址:https://www.cnblogs.com/tntboom/p/4568695.html