Boost.Foreach
BOOST_FOREACH简化了C++的循环遍历序列元素。
支持的序列类型:Boost.Range识别的序列
- STL容器
- 数组
- Null-terminated String
- std::pair of iterators
#include <string>
#include <iostream>
#include <boost/foreach.hpp> int main()
{
std::string hello( "Hello, world!" ); BOOST_FOREACH( char ch, hello )
{
std::cout << ch;
} return ;
}
遍历STL容器:
std::list<int> list_int( /*...*/ );
BOOST_FOREACH( int i, list_int )
{
// do something with i
}
遍历数组,包含short到int的协变(covariance):
short array_short[] = {,,};
BOOST_FOREACH( int i, array_short )
{
// The short was implicitly converted to an int
}
打破循环:
std::deque<int> deque_int( /*...*/ );
int i = ;
BOOST_FOREACH( i, deque_int )
{
if( i == ) return;
if( i == ) continue;
if( i == ) break;
}
遍历引用并修改:
short array_short[] = { , , };
BOOST_FOREACH( short & i, array_short )
{
++i;
}
// array_short contains {2,3,4} here
嵌套遍历:
std::vector<std::vector<int> > matrix_int;
BOOST_FOREACH( std::vector<int> & row, matrix_int )
BOOST_FOREACH( int & i, row )
++i;
直接遍历返回值:
extern std::vector<float> get_vector_float();
BOOST_FOREACH( float f, get_vector_float() )
{
// Note: get_vector_float() will be called exactly once
}
反向遍历:
std::list<int> list_int( /*...*/ );
BOOST_REVERSE_FOREACH( int i, list_int )
{
// do something with i
}
美化一下名字:
#define foreach_ BOOST_FOREACH
#define foreach_r_ BOOST_REVERSE_FOREACH
Boost.Foreach的更多相关文章
- boost常用记录
1.BOOST_FOREACH 经常会遍历容器,写for/while循环到手痛,使用BOOST_FOREACH可以减少我们的工作.支持容器vector/list/set/deque/stack/que ...
- Boost C++: 网络编程1
#include <iostream> #include <boost/asio.hpp> #include <boost/config/compiler/visualc ...
- boost::xml——基本操作以及中文乱码解决方案
下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现 ...
- boost::xml————又一次失败的尝试
尝试使用wptree来进行xml解析,又一次失败了,可以正常读取正常输出,但是使用wptree进行节点读取失败(乱码) 请看源码: DealXml.h #pragma once #include &l ...
- Boost 和 STL 相比有哪些优势和劣势?
1. 在设计原则上,STL和Boost大体统一因为STL和Boost基本上都是标准委员会那批人在策划.审核和维护,所以口味上是相对接近的.但是因为Boost并不在标准中,或者说是下一代标准的试验场,所 ...
- boost json生成和解析用法
json c++库还是有很多的,因为工作上经常使用boost,这里选用boost的json,记录下用法. 举个栗子: 如果我们要生成如下格式的json: { "name":&quo ...
- boost.xml_parser中文字符问题
当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误. 网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错.而使用ptree来写中文时不会出错. 综合 ...
- 使用boost中的property_tree实现配置文件
property_tree是专为配置文件而写,支持xml,ini和json格式文件 ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择. 使用property_tr ...
- boost解析XML方法教程
boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明 http://blog.csdn.net/luopeiyuan1990/article/details/9445691 一 ...
随机推荐
- 微软IOC容器Unity简单代码示例1
@(编程) 1. 通过Nuget下载Unity 这个就不介绍了 2. 接口代码 namespace UnityDemo { interface ILogIn { void Login(); } } 3 ...
- iOS开发中的Get请求和POST请求
//Get请求一般为不涉及到用户的账号密码的网络请求,其中Get请求是等请求内容回来之后,才可以进行下一步的操作 - (void)requestWithGet{ //Get请求: //1.设置请求路径 ...
- HDOJ_1010 Tempter of the Bone
http://acm.hdu.edu.cn/showproblem.php?pid=1010 奇偶剪枝:可以把map看成这样: 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 ...
- codeforces 659A Round House
A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Python基础 基本运算符
什么是操作符 形如 1+1 = 2 这样的 1 就是操作数 + 就是操作符 python语言支持的操作符: 算数运算符 比较运算符(关系) 赋值运算符 逻辑运算符 位运算符 会员操作符 标志操作符 算 ...
- Tun/Tap interface tutorial
Foreword: please note that the code available here is only for demonstration purposes. If you want t ...
- CSS布局经验谈
1.盒子模型 CSS最具特色也是最本质的可以浓缩成盒子模型. 整个页面可以通过大盒子套小盒子,盒子挨着盒子放,摆成一个页面即可. 盒子即所谓的块元素,只有块元素才有宽和高,有了宽和高才能使盒子挨着盒子 ...
- WOSA/XFS及SP综述
转自 http://blog.csdn.net/andyhou/article/details/6888416 前言: 写给ATM硬件和软件人员的无言歌. 希望对工作 ...
- C++学习笔记之输入、输出和文件
一.流的概念 数据从内存的一个地址移动到另一个地址称为数据流动——流操作 流操作是通过缓冲区(buffer)机制实现的. 缓冲区:内存的一块区域——用作文件与内存交换数据. 数据从文件中读出:文件 → ...
- 导出excel——入门
简单介绍 Jxl使用总结 样例 java使用jxl操作Excel文件总结