关于 Lambda 使用 boost::adaptors::filtered() 的两个问题
Two Questions About Lambda Use With boost::adaptors::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;
};
编译因此错误而终止:
ex.cc: In lambda function:
ex.cc:21:46: error: ‘tf’ is not captured
[](Stuff const& s) { return (s.var == tf); }
^
1.) 如何将函数参数带入我的 lambda?
2.) 这是一种危险的方法吗?我应该改用 std::remove_copy_if() 吗?
- 我不担心“get_all_stuff()”返回的向量的生命周期。
- 我担心“get_some_stuff()”返回的向量的生命周期。
要将外部值放入 lambda 中,您必须捕获它。
[&tf](Stuff const& s) { return (s.var == tf);
我在示例中使用了 boost::adaptors::filter
。但是 ether one 将 return 一个范围,而不是一个矢量对象。如果你想 return 一个不同于 list_of_stuff
的向量,你将不得不构建它。如果你从你的函数中 return 它,编译器会尽可能地移动它。这是一个有效的 example on coliru.
#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;
}
请考虑这段非编译代码:
#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;
};
编译因此错误而终止:
ex.cc: In lambda function:
ex.cc:21:46: error: ‘tf’ is not captured
[](Stuff const& s) { return (s.var == tf); }
^
1.) 如何将函数参数带入我的 lambda?
2.) 这是一种危险的方法吗?我应该改用 std::remove_copy_if() 吗?
- 我不担心“get_all_stuff()”返回的向量的生命周期。
- 我担心“get_some_stuff()”返回的向量的生命周期。
要将外部值放入 lambda 中,您必须捕获它。
[&tf](Stuff const& s) { return (s.var == tf);
我在示例中使用了 boost::adaptors::filter
。但是 ether one 将 return 一个范围,而不是一个矢量对象。如果你想 return 一个不同于 list_of_stuff
的向量,你将不得不构建它。如果你从你的函数中 return 它,编译器会尽可能地移动它。这是一个有效的 example on coliru.
#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;
}