在每个循环中使用C ++ boost [英] using C++ boost for each loop

查看:92
本文介绍了在每个循环中使用C ++ boost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只是在玩有用的BOOST_FOREACH循环.我遇到语法问题

Hi,

Just playing around with the useful BOOST_FOREACH loop. I have run into a syntax problem

std::vector < std::pair < int, int > > aa;
BOOST_FOREACH(std::pair<int,int>   ptr, aa)
{
              std::cout  << "hello";
}



编译错误



with the compile errors

ngonl/src/bom/ProActiveOnloadList.cpp:134:56: macro "BOOST_FOREACH" passed 3 arguments, but takes just 2
ngonl/src/bom/ProActiveOnloadList.cpp:134:56: macro "BOOST_FOREACH" passed 3 arguments, but takes just 2
ngonl/src/bom/ProActiveOnloadList.cpp: In function `void <unnamed>::rejectRebateStaff(std::vector<std::pair xmlns:std="#unknown"><com::proactiveonloadpax::segment,>, std::allocator<std::pair><com::proactiveonloadpax::segment,> > >, const COM::PublishedFlightLegDate&)':
ngonl/src/bom/ProActiveOnloadList.cpp: In function `void <unnamed>::rejectRebateStaff(std::vector<std::pair><com::proactiveonloadpax::segment,>, std::allocator<std::pair><com::proactiveonloadpax::segment,> > >, const COM::PublishedFlightLegDate&)':
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: `BOOST_FOREACH' undeclared (first use this function)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: `BOOST_FOREACH' undeclared (first use this function)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: (Each undeclared identifier is reported only once for each function it appears in.)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: (Each undeclared identifier is reported only once for each function it appears in.)
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: expected `;' before '{' token
ngonl/src/bom/ProActiveOnloadList.cpp:135: error: expected `;' before '{' token

</std::pair></std::pair></unnamed></std::pair></std::pair></unnamed>



为什么它认为我要传入3个参数,正确的语法应该是什么才能使我的迭代器正确?


我知道向量是空的,但我只想在BOOST_FOREACH循环上使用正确的语法.

我应该补充一点,如果我有



Why does it think I am passing in 3 arguments and what should the correct syntax be to get my iterator correct?


I know the vector is empty but I just want the syntax right on the BOOST_FOREACH loop.

I should add that if I have

std::vector<std::pair <int, int> >::iterator  anything;



它可以正常编译,但我认为在BOOST_FOREACH上您不包含std :: vector位.

感谢您的任何输入



it compiles fine but I think on BOOST_FOREACH you don''t include the std::vector bit.

Thank you for any input

推荐答案

仅进行语法测试:):
Just for a syntax test :) :
std::vector < std::pair < int, int > > aa(/*..*/);
BOOST_FOREACH(std::pair < int, int >/*&*/ pair, aa)
{
  std::cout  << "hello";
}


我设法使其正常工作...

< pre lang ="c ++">
std :: vector< std :: pair< int,int> > aa;
//std :: vector< std :: pair< int,int> > :: iterator afasdf;
std :: pair< int,int> pp;
BOOST_FOREACH(pp,aa)
{
std :: cout<< 你好";
}


</pre>

但我不知道为什么会起作用.....

仍然可以继续编写代码.
I managed to get it to work...

<pre lang="c++">
std::vector<std::pair <int, int> > aa;
// std::vector<std::pair <int, int> >::iterator afasdf;
std::pair < int, int > pp ;
BOOST_FOREACH(pp , aa)
{
std::cout << "hello";
}


</pre>

but I dont know why it works.....

Still I can plod on with my coding.


您是否在Boost上签出了此链接:

boost foreach [
Have you checked out this link on Boost:

boost foreach[^]

Otherwise, in your second entry, the foreach command needs a variable to receive the output from your container, as well as the instance of the container. You have declared a pair<> object pp to be the variable, and accept each value iterated in the vector aa.

If you want to modify the values in your vector, you would need to use a reference, &pp.


这篇关于在每个循环中使用C ++ boost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆