阅读量:2
当然可以!在C++中,你可以使用std::set的迭代器来遍历集合,并使用std::greater来实现降序遍历
#include
#include
#include
int main() {
std::set<int> my_set = {5, 1, 9, 3, 7};
// 使用 std::greater 作为比较函数对象,实现降序遍历
for (auto it = my_set.rbegin(); it != my_set.rend(); ++it) {
std::cout << *it << " ";
}
return 0;
}
在这个示例中,我们使用了rbegin()和rend()方法来获取反向迭代器,然后使用std::greater作为比较函数对象来实现降序遍历。输出结果将是:
9 7 5 3 1
以上就是关于“c++ set遍历 能按降序吗”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm