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. 自己制作 SPx N合1 自动安装盘(x86)

    来处"xinso" 一.制作方法: 以技嘉和惠普为例作,其它的可以如法泡制及变通: 1.复制一份最常用的 OEM XP,例如技嘉,到D:\1TO2 2.在 D:\ 创造一个 HP ...

  2. datagridview自动填充列头

    //填充datagridview dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

  3. lght oj 1257 - Farthest Nodes in a Tree (II) (树dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1257 跟hdu2196一样,两次dfs //#pragma comment(l ...

  4. 将UIImage保存成JPG或PNG格式存储在本地

    -(void)pngAndJpg:(UIImage*)image{ NSString *pngPath = [NSHomeDirectory() stringByAppendingPathCompon ...

  5. lua安装和简单使用

    1.安装 下载地址:https://www.lua.org/download.html 编译之前要安装readline,直接用yum安装 yum -y install readline-devel n ...

  6. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]

    3.5 Delete删除用户 删除也是通过ObjectID获得对象进行删除 [Authorize] public async Task<ActionResult> Delete(strin ...

  7. Nginx系列~Nginx服务启动不了

    Nginx服务有时起动不了了,原因是80端口为其它应用程序占用了,这时,我们需要查看是哪个程序占用了它,可能是IIS的某个站点,或者Tomat,Apache等,都有可能,所以,我们需要查看一下电脑80 ...

  8. Mac OS X取消Apache(httpd)开机启动

    安装MAMP后,启动服务时提示Apache启动失败,80端口被占用.查看进程发现存在几个httpd. OS X自带Apache,可是默认是没有启动的.我也没有开启Web共享,怎么就开机启动了呢? 不知 ...

  9. A debugger is already attached

    Today is the last day that all the laptops of winXP OS should be upgrade to WIN7. After updated. whe ...

  10. Android自定义图形,图形的拼接、叠加、相容

    直接上Xfermode子类: AvoidXfermode  指定了一个颜色和容差,强制Paint避免在它上面绘图(或者只在它上面绘图). PixelXorXfermode  当覆盖已有的颜色时,应用一 ...