本文整理汇总了C++中iter::filter方法的典型用法代码示例。如果您正苦于以下问题:C++ iter::filter方法的具体用法?C++ iter::filter怎么用?C++ iter::filter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类iter
的用法示例。
在下文中一共展示了iter::filter方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
std::vector<int> vec{1, 5, 6, 7, 2, 3, 8, 3, 2, 1};
std::cout << "Greater than 4 (function pointer)\n";
for (auto i : filter(greater_than_four, vec)) {
std::cout << i << '\n';
}
std::cout << "Less than 4 (lambda)\n";
for (auto i : filter([] (const int i) { return i < 4; }, vec)) {
std::cout << i << '\n';
}
LessThanValue lv(4);
std::cout << "Less than 4 (callable object)\n";
for (auto i : filter(lv, vec)) {
std::cout << i << '\n';
}
std::cout << "Nonzero ints filter(vec2)\n";
std::vector<int> vec2 {0, 1, 2, 0, 3, 0, 0, 0, 4, 5, 0};
for (auto i : filter(vec2)) {
std::cout << i << '\n';
}
std::cout << "odd numbers in range(10) temp\n";
for (auto i : filter([] (const int i) {return i % 2;}, iter::range(10))) {
std::cout << i << '\n';
}
std::cout << "range(-1, 2)\n";
for (auto i : filter(iter::range(-1, 2))) {
std::cout << i << '\n';
}
std::cout << "ever numbers in initializer_list\n";
for (auto i : filter([] (const int i) {return i % 2 == 0;},
{1, 2, 3, 4, 5, 6, 7}))
{
std::cout << i << '\n';
}
std::cout << "default in initialization_list\n";
for (auto i : filter({-2, -1, 0, 0, 0, 1, 2})) {
std::cout << i << '\n';
}
std::cout << "ever numbers in vector temporary\n";
for (auto i : filter([] (const int i) {return i % 2 == 0;},
std::vector<int>{1, 2, 3, 4, 5, 6, 7}))
{
std::cout << i << '\n';
}
return 0;
}
示例2: TEST_CASE
auto dec_ten = [](MyUnMovable& el) -> MyUnMovable& {
int va = el.get_val();
el.set_val(va - 10);
return el;
};
}
TEST_CASE("filtering doesn't dereference multiple times", "[imap][filter]") {
using iter::filter;
using iter::imap;
// source data
std::array<MyUnMovable, 3> arr = {{{41}, {42}, {43}}};
auto transformed1 = imap(inc_ten, arr);
auto filtered = filter(
[](const MyUnMovable& el) { return 52 != el.get_val(); }, transformed1);
auto transformed2 = imap(dec_ten, filtered);
std::vector<int> v;
for (auto&& el : transformed2) {
// I would use imap again instead of the loop if this wasn't an imap
// test
v.push_back(el.get_val());
}
std::vector<int> vc = {41, 43};
REQUIRE(v == vc);
constexpr std::array<MyUnMovable, 3> arrc = {{{41}, {52}, {43}}};
REQUIRE(arr == arrc);
示例3: operator
int compare_val;
public:
LessThanValue(int v) : compare_val(v) { }
bool operator() (int i) {
return i < this->compare_val;
}
};
}
TEST_CASE("filter: handles different functor types", "[filter]") {
Vec ns = {1,2, 5,6, 3,1, 7, -1, 5};
Vec vc = {1,2,3,1,-1};
SECTION("with function pointer") {
auto f = filter(less_than_five, ns);
Vec v(std::begin(f), std::end(f));
REQUIRE( v == vc );
}
SECTION("with callable object") {
auto f = filter(LessThanValue{5}, ns);
Vec v(std::begin(f), std::end(f));
REQUIRE( v == vc );
}
SECTION("with lambda") {
auto ltf = [](int i) {return i < 5;};
auto f = filter(ltf, ns);
Vec v(std::begin(f), std::end(f));
REQUIRE( v == vc );