std::vector<int> numbers {22, 7, 93, 45, 19}; std::vector<double> data {1.5, 2.5, 3.5, 4.5}; std::cout << "numbers is "<<(std::is_sorted(std::begin (numbers), std::end(numbers)) ? "": "not ") << "in ascending sequence.n"; std::cout << "data is "<< (std::is_sorted(std::begin(data), std::end(data)) ?"":"not ") << "in ascending sequence." << std::endl;預設使用的比較函數是 < 運算子。鑑於“data is”的輸出,表明 numbers 不是一個升序序列。還有一個版本也可以指定用於比較元素的函數物件:
std:: cout << "data reversed is "<< (std::is_sorted(std::rbegin(data), std::rend(data), std::greater<>()) ? "":"not ")<< "in descending sequence." << std::endl;這條語句的輸出說明 data 中元素的逆序是降序。
std::vector<string> pets {"cat", "chicken", "dog", "pig", "llama", "coati", "goat"}; std::cout << "The pets in ascending sequence are:n"; std::copy(std::begin(pets), std::is_sorted_until(std::begin(pets), std::end (pets)) , std::ostream_iterator<string>{ std::cout," "});copy() 演算法的前兩個引數分別是pets容器的開始疊代器以及當 is_sorted_until() 應用到 pets 中全部元素上時返回的疊代器。is_sorted_until() 演算法會返回指向 pets 中升序序列元素的上邊界,它是指向小於其前面元素的第一個元素的疊代器。如果序列是有序的,則返回一個結束疊代器。這段程式碼的輸出如下:
The pets in ascending sequence are:
cat chicken dog pig
std::vector<string> pets {"dog", "coati", "cat", "chicken", "pig", "llama", "goat"}; std::cout << "The pets in descending sequence are:n"; std::copy(std::begin(pets),std::is_sorted_until(std::begin(pets), std::end (pets) , std::greater<>()),std::ostream_iterator<string>{ std::cout," "});這一次我們會查詢降序元素,因為使用的是 string 類的成員函數 operator>() 來比較元素。輸出為:
The pets in descending sequence are:
dog coati cat