Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++11 关于Lambda与boost::adapters::filtered()一起使用的两个问题 请考虑此非编译代码: #include <boost/range/adaptors.hpp> class Stuff { public: bool var; }; class Manager { /// Get everything std::vector<Stuff*> get_all_stuff() const { return list_of_stuff; } /// Get a vector of only those that whose "var" matches the "tf" argument. std::vector<Stuff*> get_some_stuff(const bool tf) const { return (get_all_stuff() | boost::adaptors::filtered( [](Stuff const& s) { return (s.var == tf); } ) ); } private: std::vector<Stuff*> list_of_stuff; };_C++11_Boost_Boost Adaptors - Fatal编程技术网

C++11 关于Lambda与boost::adapters::filtered()一起使用的两个问题 请考虑此非编译代码: #include <boost/range/adaptors.hpp> class Stuff { public: bool var; }; class Manager { /// Get everything std::vector<Stuff*> get_all_stuff() const { return list_of_stuff; } /// Get a vector of only those that whose "var" matches the "tf" argument. std::vector<Stuff*> get_some_stuff(const bool tf) const { return (get_all_stuff() | boost::adaptors::filtered( [](Stuff const& s) { return (s.var == tf); } ) ); } private: std::vector<Stuff*> list_of_stuff; };

C++11 关于Lambda与boost::adapters::filtered()一起使用的两个问题 请考虑此非编译代码: #include <boost/range/adaptors.hpp> class Stuff { public: bool var; }; class Manager { /// Get everything std::vector<Stuff*> get_all_stuff() const { return list_of_stuff; } /// Get a vector of only those that whose "var" matches the "tf" argument. std::vector<Stuff*> get_some_stuff(const bool tf) const { return (get_all_stuff() | boost::adaptors::filtered( [](Stuff const& s) { return (s.var == tf); } ) ); } private: std::vector<Stuff*> list_of_stuff; };,c++11,boost,boost-adaptors,C++11,Boost,Boost Adaptors,1.)如何将该函数参数引入lambda 2.)这是一种危险的方法吗?我应该改用std::remove\u copy\u if()吗 我并不担心“get_all_stuff()”返回的向量的生命周期 我担心“get_some_stuff()”返回的向量的生命周期 要将外部值获取到lambda中,必须捕获它 [&tf](Stuff const&s){return(s.var==tf); 我在示例中使用了boost::adapters::filter。但是ether one将返回一个范围,而不是向量

1.)如何将该函数参数引入lambda

2.)这是一种危险的方法吗?我应该改用std::remove\u copy\u if()吗

  • 我并不担心“get_all_stuff()”返回的向量的生命周期
  • 我担心“get_some_stuff()”返回的向量的生命周期

要将外部值获取到lambda中,必须捕获它

[&tf](Stuff const&s){return(s.var==tf);

我在示例中使用了
boost::adapters::filter
。但是ether one将返回一个范围,而不是向量对象。如果您想返回一个与
list of_stuff
不同的向量,您必须构建它。如果您从函数返回它,编译器将尽可能移动它。下面是一个工作示例

#包括
#包括
#包括
课堂材料{
公众:
布尔变量;
int-id;
};

std::ostream&operator Thank@lakeweb!您的代码是否像您的Coliru示例所使用的那样需要C++17?我需要C++11…早上好。所以我去了,因为我可以在MS设置为默认值时不出错(我认为是C++11)。它唯一犹豫的是自动返回类型
get_all_stuff()
。将其更改为
矢量_类型
并编译。谢谢@lakeweb!
ex.cc: In lambda function:
ex.cc:21:46: error: ‘tf’ is not captured
        [](Stuff const& s) { return (s.var == tf); }
                                              ^
#include <iostream>
#include <boost/range/algorithm.hpp>
#include <boost/range/adaptors.hpp>

class Stuff {
public:
    bool var;
    int id;
};
std::ostream& operator << (std::ostream& os, const Stuff stuff) {
    return os << std::boolalpha << stuff.id << " " << stuff.var;
}
using vector_type = std::vector<Stuff>;

class Manager {
    /// Get everything
public:
    auto get_all_stuff() const
    {
        return list_of_stuff;
    }

    // Get a vector of only those that whose "var" matches the "tf" argument.
    vector_type get_some_stuff(const bool tf) const
    {
        vector_type temp;
        for (auto item : boost::adaptors::filter(list_of_stuff,
            [&tf](Stuff const& s) { return s.var == tf; }))
            temp.push_back(item);
        return temp;
    }

private:
    vector_type list_of_stuff = { {false,1},{true,2},{false,3},{true,4},{true,5} };
};
int main()
{
    Manager manage;
    for (const auto item : manage.get_all_stuff())
        std::cout << item << " ";
    std::cout << std::endl;
    for (const auto item : manage.get_some_stuff(true))
        std::cout << item << " ";
    std::cout << std::endl;
}