如何使用C ++ Boost库中的for_each? [英] How to use for_each from C++ boost library?

查看:130
本文介绍了如何使用C ++ Boost库中的for_each?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 for_each .这是我的代码.

I try to use for_each from the C++ boost library. This is the code that I have.

#include <iostream>
#include <vector>

#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/include/for_each.hpp>

using namespace std;

int main() {

        vector<int> vec;
        vec.push_back(1);
        vec.push_back(2);
        vec.push_back(3);

         boost::for_each(
                vec,
                [](int val){
                        cout << val << "\n";
                }
        );

        return 0;
}

这是我编译代码的方式:

This is how I compile my code:

g++ -std=c++0x -I /opt/software/boost/1.50_ubuntu12.4lts_gcc4.7.2/include -c try_boost.cpp
g++ -o try_boost -L/opt/software/boost/1.50_ubuntu12.4lts_gcc4.7.2/lib try_boost.o -lboost

结果我得到了:

error: ‘for_each’ is not a member of ‘boost’

有人知道为什么它不起作用吗?

Does anybody know why it does not work?

推荐答案

您正在使用Boost.Fusion库中的for_each. std::vector无效.

You're using for_each from Boost.Fusion Library. That doesn't work with std::vector.

您需要的for_each来自Boost.Range库.

The for_each which you need is from Boost.Range Library.

#include <boost/range/algorithm/for_each.hpp> //note this difference!

boost::for_each(vec, your-lambda-expression);

它在boost::range命名空间内定义,并使用using声明性引入到boost命名空间.所以你也可以这样写:

It is defined inside boost::range namespace, which is brought to boost namespace using using declarative. So you can also write this:

boost::range::for_each(vec, your-lambda-expression);

这篇关于如何使用C ++ Boost库中的for_each?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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