std::vector<double> temperatures {65, 75, 56, 48, 31, 28, 32, 29, 40, 41, 44, 50}; auto average = std::accumulate(std::begin(temperatures),std::end(temperatures), 0.0)/ temperatures.size(); auto predicate = [average](double t) { return t < average; }; std::stable_partition(std::begin(temperatures), std::end(temperatures), predicate); auto iter = std::partition_point(std::begin(temperatures),std::end(temperatures), predicate); std::cout << "Elements in the first partition: "; std::copy(std::begin(temperatures), iter,std::ostream_iterator<double>{std::cout, " "}); std::cout << "nElements in the second partition: ";std::copy(iter, std::end(temperatures),std::ostream_iterator<double>{std::cout, " "}); std::cout << std::endl;這段程式碼會相對於平均溫度對 temperatures 中的元素進行分割區,並通過呼叫 partition_point() 找到這個序列的分割區點。這個分割區點是第一個分割區的結束疊代器,它被儲存在 iter 中。所以 [std::begin(temperatures),iter) 對應的就是第一個分割區中的元素,[iter,std::end(temperatures)) 包含的是第二個分割區中的元素。這裡使用兩次 copy() 演算法來輸出分割區,輸出內容如下:
Elements in the first partition: 31 28 32 29 40 41 44
Elements in the second partition: 65 75 56 48 50
if(std::is_partitioned(std::begin(temperatures), std::end(temperatures),[average](double t) { return t < average; })) { auto iter = std::partition_point(std::begin(temperatures),std::end(temperatures), [average](double t) { return t < average; }); std::cout << "Elements in the first partition: "; std::copy(std::begin(temperatures), iter,std::ostream_iterator<double>{std::cout, " " }); std::cout << "nElements in the second partition: "; std::copy(iter, std::end(temperatures),std::ostream_iterator<double>{std::cout," "}); std::cout << std::endl; } else std::cout << "Range is not partitioned." << std::endl;只有在 is_partitioned() 返回 true 時,這段程式碼才會執行。如果 if 語句為 true,iter 變數會指向分割區點。如果想在後面繼續使用 iter,可以按如下方式在 if 語句之前定義它:
std::vector<double>::iterator iter;在所有容器型別的模板中都定義了這樣的疊代器型別別名,它們使疊代器變得可以使用。它對應於這個容器型別的成員函數 begin() 和 end() 所返回的疊代器型別。