C++ 的 STL sort
等级 4204 次按键
用 sort 将 vector 按升序排序,然后传入 greater<int>() 作为比较器按降序排序。
C++ 代码
1vector<int> nums = {5, 2, 8, 1, 9};// 将整个范围按升序排序(默认使用 operator<)2sort(nums.begin(), nums.end());// 输出 1 2 5 8 9,每个数字后面带一个空格3for (int n : nums) cout << n << " ";// 结束第一行4cout << endl;5 // greater<int>() 使用 > 比较,因此变为降序6sort(nums.begin(), nums.end(), greater<int>());// 输出 9 8 5 2 17for (int n : nums) cout << n << " ";运行结果
1 2 5 8 9 9 8 5 2 1
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 C++ 代码中需要输入的符号及其出现次数。
<× 12(× 9)× 9,× 7;× 6.× 4"× 4:× 2>× 2{× 1