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 同学分开祝福语简短
14 妈妈生日贺卡祝福语 简短
15 婚礼仪式祝福语简短
16 步入高中祝福语简短励志
17 新年探亲祝福语简短精辟
18 对司机的祝福语简短