forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarmap_examples.cpp
More file actions
40 lines (35 loc) · 1.26 KB
/
Copy pathstarmap_examples.cpp
File metadata and controls
40 lines (35 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <starmap.hpp>
#include <array>
#include <tuple>
#include <vector>
#include <iostream>
// Since this is an example file I'm dumping a bunch of using declarations
// here, in real code I use using declarations very sparingly
using std::pair;
using std::tuple;
using std::vector;
using std::make_pair;
using std::make_tuple;
struct Callable {
int operator()(int i) const { return i; }
int operator()(int i, char c) const { return i + c; }
int operator()(unsigned int u, int i, char c) const { return i + c + u; }
};
int main() {
// the function will be called with the tuple-like object unpacked
// using std::get. This means an iterable of tuples, arrays, pairs, or
// whatever works with std::get
vector<pair<int, int>> v = {{2, 3}, {5, 2}, {3, 4}}; // {base, exponent}
for (auto&& i : iter::starmap([](int b, int e){ return b * e; }, v)) {
std::cout << i << '\n';
}
// Alternatively if an object has multiple call operators, a tuple-like
// object of tuple-like objects
auto t = make_tuple(
make_tuple(5), // first form
make_pair(3, 'c'), // second form
make_tuple(1u, 1, '1')); // third form
for (auto&& i : iter::starmap(Callable{}, t)) {
std::cout << i << '\n';
}
}