C++ 算法 中的 is_sorted函数

首页 / C++入门教程 / C++ 算法 中的 is_sorted函数

如果参数[first,last)中的元素按升序排序,则C++算法 is_sorted()函数将返回true。

is_sorted - 语法

default (1)	template <class ForwardIterator>
                    bool is_sorted (ForwardIterator first, ForwardIterator last);

custom (2)	 template <class ForwardIterator, class Compare>
 	       bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);

is_sorted - 参数

first:一个正向迭代器,指向要检查参数内的第一个元素。

last:一个随机访问迭代器,它指向要检查参数内的最后一个最后一个元素。

comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数按顺序排列,则返回true,否则返回false。

is_sorted - 返回值

如果参数[first,last)按升序排序,则返回true,否则返回false。

is_sorted - 例子1

让我们看一个简单的示例来演示is_sorted()的用法:

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-is-sorted-function.html

来源:LearnFk无涯教程网

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {3, 1, 4, 2, 5};

  cout << std::boolalpha;
  cout << "before sorting: is sorted? " << is_sorted(v.begin(), v.end()) << endl;

  sort(v.begin(), v.end());

  cout << "after sorting : is sorted? " << is_sorted(v.begin(), v.end()) << endl;
  
  return 0;
}

输出:

before sorting: is sorted? false
after sorting : is sorted? True

is_sorted - 例子2

让我们看另一个简单的例子:

#include <iostream>    //std::cout
#include <algorithm>   //std::is_sorted, std::prev_permutation
#include <array>       //std::array

using namespace std;

int main () {
  array<int,5> a {2,4,1,3,5};

  do {
   //try a new permutation:
    prev_permutation(a.begin(),a.end());

   //print range:
    cout << "a:";
    for (int& x:a) cout << ' ' << x;
    cout << '\n';

  } while (!is_sorted(a.begin(),a.end()));

  cout << "the range is sorted!\n";

  return 0;
}

输出:

a: 2 3 5 4 1
a: 2 3 5 1 4
a: 2 3 4 5 1
a: 2 3 4 1 5
a: 2 3 1 5 4
a: 2 3 1 4 5
a: 2 1 5 4 3
a: 2 1 5 3 4
a: 2 1 4 5 3
a: 2 1 4 3 5
a: 2 1 3 5 4
a: 2 1 3 4 5
a: 1 5 4 3 2
a: 1 5 4 2 3
a: 1 5 3 4 2
a: 1 5 3 2 4
a: 1 5 2 4 3
a: 1 5 2 3 4
a: 1 4 5 3 2
a: 1 4 5 2 3
a: 1 4 3 5 2
a: 1 4 3 2 5
a: 1 4 2 5 3
a: 1 4 2 3 5
a: 1 3 5 4 2
a: 1 3 5 2 4
a: 1 3 4 5 2
a: 1 3 4 2 5
a: 1 3 2 5 4
a: 1 3 2 4 5
a: 1 2 5 4 3
a: 1 2 5 3 4
a: 1 2 4 5 3
a: 1 2 4 3 5
a: 1 2 3 5 4
a: 1 2 3 4 5
the range is sorted!

上面的示例显示了排序顺序并打印元素,直到对其进行排序。

is_sorted - 例子3

让我们看另一个简单的示例,以检查元素是否已排序:

#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

int main() 
{
    int digits[] = {3, 1, 4, 1, 5};
 
    for (auto i : digits) cout << i << ' ';
    cout << ": is_sorted: " << boolalpha
              << is_sorted(begin(digits), end(digits)) << '\n';
 
    sort(begin(digits), end(digits));
 
    for (auto i : digits) cout << i << ' ';
    cout << ": is_sorted: "
              << is_sorted(begin(digits), end(digits)) << '\n';
              
    return 0;
}

输出:

3 1 4 1 5 : is_sorted: false
1 1 3 4 5 : is_sorted: true

is_sorted - 例子4

让我们看另一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool ignore_case(char a, char b) {
   return (tolower(a) == tolower(b));
}

int main(void) {
   vector<char> v = {'D', 'b', 'C', 'p', 'N'};
   bool result;

   result = is_sorted(v.begin(), v.end());

   if (result == false)
      cout << "Vector elements are not sorted in ascending order." << endl;

   result = is_sorted(v.begin(), v.end(), ignore_case);

   if (result == true)
      cout << "Vector elements are sorted in ascending order." << endl;

   return 0;
}

输出:

Vector elements are not sorted in ascending order.
Vector elements are sorted in ascending order.

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

推荐系统三十六式 -〔刑无刀〕

软件测试52讲 -〔茹炳晟〕

Go语言从入门到实战 -〔蔡超〕

OpenResty从入门到实战 -〔温铭〕

React Hooks 核心原理与实战 -〔王沛〕

结构学习力 -〔李忠秋〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

Rust 语言从入门到实战 -〔唐刚〕

手把手带你写一个 MiniTomcat -〔郭屹〕

好记忆不如烂笔头。留下您的足迹吧 :)