我有一个 std::deque< std::pair<int, int> >
我想使用 BOOST_FOREACH
进行迭代.
我尝试了以下方法:
#define foreach_ BOOST_FOREACH
// declaration of the std::deque
std::deque< std::pair<int, int> > chosen;
foreach_( std::pair<int,int> p, chosen )
{
...
}
但是当我编译它时(在 Visual Studio 中)我得到以下错误:
warning C4002: too many actual parameters for macro 'BOOST_FOREACH'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ')' before '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : '>'
1>c:\users\beeband\tests.cpp(133): error C2059: syntax error : ')'
1>c:\users\beeband\tests.cpp(133): error C2143: syntax error : missing ';' before '{'
1>c:\users\beeband\tests.cpp(133): error C2181: illegal else without matching if
正确的使用方法是什么BOOST_FOREACH
有了这个deque
?
最佳答案
问题出在 ,
上,因为预处理器使用它来分隔宏参数。
使用 typedef
的可能解决方案:
typedef std::pair<int, int> int_pair_t;
std::deque<int_pair_t> chosen;
foreach_( int_pair_t p, chosen )
// Or (as commented by Arne Mertz)
typedef std::deque<std::pair<int, int>> container_t;
container_t chosen;
foreach_(container_t::value_type p, chosen)
C++11 中引入的可能的替代品是:
关于c++ - 将 boost foreach 与本身就是模板的项目一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17467683/