大佬教程收集整理的这篇文章主要介绍了C++ is_sorted(STL is_sorted)排序算法详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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","cat","goat"}; std::cout << "The pets in descending sequence are:\n"; std::copy(std::begin(pets),std::end (pets),std::greater<>())," "});这一次我们会查找降序元素,因为使用的是 String 类的成员函数 operator>() 来比较元素。输出为:
The pets in descending sequence are:
dog coati cat
以上是大佬教程为你收集整理的C++ is_sorted(STL is_sorted)排序算法详解全部内容,希望文章能够帮你解决C++ is_sorted(STL is_sorted)排序算法详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。