本文整理汇总了C++中iter::takewhile方法的典型用法代码示例。如果您正苦于以下问题:C++ iter::takewhile方法的具体用法?C++ iter::takewhile怎么用?C++ iter::takewhile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类iter
的用法示例。
在下文中一共展示了iter::takewhile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1};
for (auto i : takewhile([] (int i) {return i < 5;}, ivec)) {
std::cout << i << '\n';
}
for (auto i : takewhile([] (int i) {return i < 5;}, range(10))) {
std::cout << i << '\n';
}
for (auto i : takewhile([] (int i) {return i < 5;},
{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
std::cout << i << '\n';
}
std::cout << "with temporary\n";
for (auto i : takewhile([] (int i) {return i < 5;},
std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
std::cout << i << '\n';
}
return 0;
}
示例2: under_ten
bool under_ten(int i) {
return i < 10;
}
struct UnderTen {
bool operator()(int i) {
return i < 10;
}
};
}
TEST_CASE("takewhile: works with lambda, callable, and function pointer",
"[takewhile]") {
Vec ns = {1, 3, 5, 20, 2, 4, 6, 8};
SECTION("function pointer") {
auto tw = takewhile(under_ten, ns);
Vec v(std::begin(tw), std::end(tw));
Vec vc = {1, 3, 5};
REQUIRE(v == vc);
}
SECTION("callable object") {
std::vector<int> v;
SECTION("Normal call") {
auto tw = takewhile(UnderTen{}, ns);
v.assign(std::begin(tw), std::end(tw));
}
SECTION("Pipe") {
auto tw = ns | takewhile(UnderTen{});
v.assign(std::begin(tw), std::end(tw));