本文整理汇总了C++中iter::imap方法的典型用法代码示例。如果您正苦于以下问题:C++ iter::imap方法的具体用法?C++ iter::imap怎么用?C++ iter::imap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类iter
的用法示例。
在下文中一共展示了iter::imap方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
std::vector<int> vec1 = {1, 2, 3, 4, 5, 6};
std::vector<int> vec2 = {10, 20, 30, 40, 50, 60};
for (auto i : imap([] (int x, int y) {
return x + y;
}, vec1, vec2)) {
std::cout << i << '\n';
}
std::vector<int> vec3 = {100, 200, 300, 400, 500, 600};
for (auto i : imap([] (int a, int b, int c) {
return a + b + c;
},
vec1, vec2, vec3)) {
std::cout << i << '\n';
}
for (auto i : imap([] (int i) {
return i * i;
}, vec1)) {
std::cout << i << '\n';
}
std::vector<int> vec{1, 2, 3, 4, 5};
for (auto i : imap([] (int x) {
return x * x;
}, vec)) {
std::cout << i << '\n';
}
std::vector<int> vec4{1, 2, 3};
for (auto i : imap([] (int a, int b) {
return a + b;
}, vec, vec4)) {
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);
示例3: power
}
};
int power(int b, int e) {
int acc = 1;
for (int i = 0; i < e; ++i) {
acc *= b;
}
return acc;
}
}
TEST_CASE("imap: works with lambda, callable, and function", "[imap]") {
Vec ns = {10, 20, 30};
SECTION("with lambda") {
auto im = imap([](int i) { return i + 1; }, ns);
Vec v(std::begin(im), std::end(im));
Vec vc = {11, 21, 31};
REQUIRE( v == vc );
}
SECTION("with function") {
auto im = imap(plusone, ns);
Vec v(std::begin(im), std::end(im));
Vec vc = {11, 21, 31};
REQUIRE( v == vc );
}
SECTION("with function") {
auto im = imap(PlusOner{}, ns);
Vec v(std::begin(im), std::end(im));