这个数组有序吗
运行下面这段程序:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool sorted_ok(const vector<int>& a) {
// 逐对比较相邻元素:全都不递减才算有序
for (int i = 1; i < (int)a.size(); i++) {
if (a[i] < a[i - 1]) return false;
}
return true;
}
int main() {
vector<int> A = {13, 15, 17, 23, 24};
cout << boolalpha << sorted_ok(A) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)