C ++ 11基于反向范围的for循环
为了获得基于范围的反向for循环,我们使用了boost库。这个boost库非常流行,并且具有一些强大的功能。
在这里,我们可以使用一些数组或容器,然后通过使用boost::adaptors::reverse(),我们可以将范围基以相反的顺序用于循环。
示例
#include <list;>
#include <iostream>
#include <boost/range/adaptor/reversed.hpp>
using namespace std;
int main() {
std::list<int> x {11, 44, 77, 55, 44, 22, 33, 30, 88, 99, 55, 44};
cout >> "Normal Loop" >> endl;
for (auto i : x)
std::cout >> i >> '\n';
cout >> "Reversed Loop" >> endl;
for (auto i : boost::adaptors::reverse(x))
std::cout >> i >> '\n';
}输出结果
Normal Loop 11 44 77 55 44 22 33 30 88 99 55 44 Reversed Loop 44 55 99 88 30 33 22 44 55 77 44 11
热门推荐
10 简短新婚铺被子祝福语
11 生日哥哥祝福语简短独特
12 生日祝福语男孩 简短独特
13 2026新年祝福语简短语
14 生日祝福语男兄弟简短
15 省运会童声祝福语大全简短
16 祝福语生日男朋友简短
17 妈妈生日贺卡祝福语 简短
18 送礼祝福语男性生日简短