标签:
example 1:
string str("Hello, world!");
BOOST_FOREACH(char c, str)
{
cout << c;
}
它相当于:
string str("Hello, world!");
for(int i = 0; i < str.length(); ++i)
{
char c = str[i];
cout << c;
}
example 2:
int arr[] = {1, 3, 5, 2, 0};
BOOST_FOREACH(int & a, arr)
{
++a;
....
}
arr中的值被改变了,如果是 int a,则arr中的值不会变。
它相当于:
int arr[] = {1, 3, 5, 2, 0};
for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i)
{
int & a = arr[i];
++a;
...
}
example 3:
std::vector<std::vector > matrix_int;
BOOST_FOREACH( std::vector & row, matrix_int )
BOOST_FOREACH( int & i, row )
++i;
to make the BOOST_FOREACH shorter:
#define foreach BOOST_FOREACH
#define rforeach BOOST_REVERSE_FOREACH
example 4:(我的程序中使用的)
ptree treeArray = pt.get_child("result").get_child("data"); // get_child得到数组对象
BOOST_FOREACH(boost::property_tree::ptree::value_type &v, treeArray) // 遍历数组
{
ptree p = v.second;
locationInfo.m_dLON[count] = p.get<double>("LNG");
locationInfo.m_dLat[count] = p.get<double>("LAT");
locationInfo.m_nPrecision[count] = p.get<int>("PRECISION");
locationInfo.m_strAddr[count] = p.get<std::string>("ADDRESS");
return true;
}
它其实就是将第二个参数循环的赋值给第一个参数。只要第二个参数是序列化的容器,如数组、string、map等。BOOST_FOREACH
支持所有序列式容器。
参考:http://blog.csdn.net/huang_xw/article/details/8451442
http://blog.csdn.net/limuyun/article/details/5622513
http://www.cnblogs.com/TianFang/archive/2008/06/24/1229218.html
inlude when using it, of course.
It‘s so easy!
标签:
原文地址:http://www.cnblogs.com/txf1949/p/4683561.html