auto pr = std::equal_range(std::begin(values) , std::end(values), wanted); std::cout << "the lower bound for " << wanted << " is " << *pr.first << std::endl; std::cout << "Tthe upper bound for " << wanted << " is " << *pr.second << std::endl;它和前一段程式碼的輸出完全相同。和前面的二分查詢演算法一樣,equal_range() 也有一個有額外引數的版本,這個引數可以為有序序列提供一些不同於 < 運算子的比較。
std::list<int> values {17, 11, 40, 36, 22, 54, 48, 70, 61, 82, 78, 89, 99, 92, 43}; // Output the elements in original order std::copy(std::begin(values), std::end(values),std::ostream_iterator<int> {std::cout, " "}); std::cout << std::endl; int wanted {22}; // What we are looking for std::partition(std::begin(values), std::end(values),[wanted](double value) { return value < wanted; }); std::partition(std::begin(values), std::end(values),[wanted](double value) { return !(wanted < value); }); //Output the elements after partitioning std::copy(std::begin(values), std::end(values),std::ostream_iterator<int>{std::cout," "}); std::cout<< std::endl;這段程式碼的輸出如下:
17 11 40 36 22 54 48 70 61 82 78 89 99 92 43
17 11 22 36 40 54 48 70 61 82 78 89 99 92 43
auto pr = std::equal_range(std::begin(values), std::end(values), wanted); std::cout << "the lower bound for " << wanted << " is " << *pr.first << std::endl; std::cout << "the upper bound for " << wanted << " is " << *pr.second << std::endl;這段程式碼和前一段程式碼的輸出是相同的,在前一段程式碼中,用容器物件的成員函數 sort() 對元素進行了完全排序。本節的所有演算法都可以用於以這種方式分割區的序列上。顯然,如果分割區使用的是>,那麼在查詢演算法中使用的函數物件也必須和它保持一致。
// Using partition() and equal_range() to find duplicates of a value in a range #include <iostream> // For standard streams #include <list> // For list container #include <algorithm> // For copy(), partition() #include <iterator> // For ostream_iterator int main() { std::list<int> values {17, 11, 40, 13, 22, 54, 48, 70, 22, 61, 82, 78, 22, 89, 99, 92, 43}; // Output the elements in their original order std::cout << "The elements in the original sequence are:n"; std::copy(std::begin(values), std::end(values), std::ostream_iterator<int> {std::cout, " "}); std::cout << std::endl; int wanted {22}; // What we are looking for std::partition(std::begin(values), std::end(values),[wanted](double value) { return value < wanted; }); std::partition(std::begin(values), std::end(values),[wanted](double value) { return !(wanted < value); }); // Output the elements after partitioning std::cout << "The elements after partitioning are:n"; std::copy(std::begin(values), std::end(values), std::ostream_iterator<int> {std::cout, " "}); std::cout << std::endl; auto pr = std::equal_range(std::begin(values), std::end(values), wanted); std::cout << "The lower bound for " << wanted << " is " << *pr.first << std::endl; std::cout << "The upper bound for " << wanted << " is " << *pr.second << std::endl; std::cout << "nThe elements found by equal_range() are:n"; std::copy(pr.first, pr.second, std::ostream_iterator<int> {std::cout, " "}); std::cout << std::endl; }
The elements in the original sequence are:
17 11 40 13 22 54 48 70 22 61 82 78 22 89 99 92 43
The elements after partitioning are:
17 11 13 22 22 22 48 70 54 61 82 78 40 89 99 92 43
The lower bound for 22 is 22
The upper bound for 22 is 48
The elements found by equal_range() are:
22 22 22
17 11 13 40 22 54 48 70 22 61 82 78 22 89 99 92 43
17、11、13 是僅有的小於 wanted 的幾個值,它們顯然在左分割區中。分割區並不能以任何特別的方式來確定和 wanted 所對應值的位置。22 的全部範例可能出現在右分割區的任何位置。17 11 13 22 22 22 48 70 54 61 82 78 40 89 99 92 43
equal_range() 找到的下邊界指向 22 的第一個匹配項,上邊界指向 22 的最後一個匹配項的後面,即值為 48 的元素。