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的更多相关文章

  1. boost常用记录

    1.BOOST_FOREACH 经常会遍历容器,写for/while循环到手痛,使用BOOST_FOREACH可以减少我们的工作.支持容器vector/list/set/deque/stack/que ...

  2. Boost C++: 网络编程1

    #include <iostream> #include <boost/asio.hpp> #include <boost/config/compiler/visualc ...

  3. boost::xml——基本操作以及中文乱码解决方案

    下面是本人使用boost库的xml部分的基础操作,并且解决对于大家使用boost库读写中文xml内容出现的乱码问题. 1.实现boost库xml基本操作2.解决boost对xml中中文乱码问题3.实现 ...

  4. boost::xml————又一次失败的尝试

    尝试使用wptree来进行xml解析,又一次失败了,可以正常读取正常输出,但是使用wptree进行节点读取失败(乱码) 请看源码: DealXml.h #pragma once #include &l ...

  5. Boost 和 STL 相比有哪些优势和劣势?

    1. 在设计原则上,STL和Boost大体统一因为STL和Boost基本上都是标准委员会那批人在策划.审核和维护,所以口味上是相对接近的.但是因为Boost并不在标准中,或者说是下一代标准的试验场,所 ...

  6. boost json生成和解析用法

    json c++库还是有很多的,因为工作上经常使用boost,这里选用boost的json,记录下用法. 举个栗子: 如果我们要生成如下格式的json: { "name":&quo ...

  7. boost.xml_parser中文字符问题

    当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误. 网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错.而使用ptree来写中文时不会出错. 综合 ...

  8. 使用boost中的property_tree实现配置文件

    property_tree是专为配置文件而写,支持xml,ini和json格式文件   ini比较简单,适合简单的配置,通常可能需要保存数组,这时xml是个不错的选择.   使用property_tr ...

  9. boost解析XML方法教程

    boost库在解析XML时具有良好的性能,可操作性也很强下地址有个简单的说明 http://blog.csdn.net/luopeiyuan1990/article/details/9445691 一 ...

随机推荐

  1. HD1060Leftmost Digit

      Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission( ...

  2. [转载]关于AutoCAD.NET的辅助方法

    转载自:http://www.cnblogs.com/milian/p/3315000.html 求中点坐标: /// <summary> /// 中点 /// </summary& ...

  3. 【转载】Windows/Office“神key的来源”(附win8神key)

        凡是没有经过微软授权的渠道激活Windows/Office的全部是"D版"!但由于密钥激活更方便快捷,因此很受欢迎.从百度博客到现在,很多网友询问:"神key&q ...

  4. 【Oracle 函数索引】一次数据库的优化过程

    [问题]表里数据2万条,查询执行时间 818087.38 ms(12分钟). SQL语句如下:select   F1,F2,F3,F4   from t_sms_g_send t   left joi ...

  5. c++常见输入方法[持续更新]

    字符串输入 cin>> 使用空格确认字符串结束位置,保留换行符在输入队列当中 单个字符读取 cin.get(), cin.get(char) 每次读取一行: cin.getline(cha ...

  6. 一个简单的Java程序例子以及其几种注释

    在说道主题前,先来啰嗦两句,o()︿︶)o 唉,不说两句心里就有个疙瘩,也许这就是所谓的强迫症吧,好了说说我想啰嗦的,其实也就是这样子的,关于Java开发工具箱的下载以及环境的配置.Java开发工具箱 ...

  7. 不用FTP使用SecureCRT上传下载文件,并解决rz、sz command not found异常

    使用SSH终端操作Linux/UNIX时,很多时候需要传一些文件到服务器上,或说从服务器上下载一些文件,这类文件传输动作一般使用FTP即可,但是需要架设FTP Server,每次传输不太方便,还要另外 ...

  8. 微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!

    [导语]我们希望能够对一些个人开发人员进行专訪,这样大家更能显得接地气,看看人家做什么,怎么坚持.<甜心爱消除>作者Lee是三群的兄弟,也关注微价值.微价值的文章还是能够的,得到一些业内大 ...

  9. arp:地址解析协议(Address Resolution Protocol)(来自维基百科)

    地址解析协议(Address Resolution Protocol),其基本功能为通过目标设备的IP地址,查询目标设备的MAC地址,以保证通信的顺利进行。它是IPv4中网络层必不可少的协议,不过在I ...

  10. Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs

    B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...