Created
February 6, 2021 12:57
-
-
Save hidez8891/c3b22a9c3fbe1adeb0eeceb703973c82 to your computer and use it in GitHub Desktop.
C++でのfilter/map操作 (C++98~C++20, range-v3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <iostream> | |
#include <iterator> | |
#include <vector> | |
// after C++20 | |
#if __cplusplus >= 202002L | |
#include <ranges> | |
#endif | |
// range-v3 | |
#ifdef USE_RANGE_V3 | |
#include <range/v3/all.hpp> | |
#endif | |
void print(const std::vector<double> &output) | |
{ | |
for (int i = 0; i < output.size(); ++i) | |
{ | |
std::cout << output[i] << " "; | |
} | |
std::cout << std::endl; | |
} | |
int main() | |
{ | |
// basic | |
{ | |
std::vector<int> input; /* {1, 2, 3, 4, 5, 6} */ | |
std::vector<int> tmp; | |
std::vector<double> output; /* {4, 8, 12} */ | |
input.push_back(1); | |
input.push_back(2); | |
input.push_back(3); | |
input.push_back(4); | |
input.push_back(5); | |
input.push_back(6); | |
// filter | |
for (int i = 0; i < input.size(); ++i) | |
{ | |
if (input[i] % 2 == 0) | |
{ | |
tmp.push_back(input[i]); | |
} | |
} | |
// map | |
for (int i = 0; i < tmp.size(); ++i) | |
{ | |
output.push_back(tmp[i] * 2.0f); | |
} | |
print(output); | |
} | |
// after C++11 | |
#if __cplusplus >= 201103L | |
{ | |
std::vector<int> input = {1, 2, 3, 4, 5, 6}; | |
std::vector<double> output; /* {4, 8, 12} */ | |
auto even = [](int it) { return it % 2 == 0; }; | |
auto square = [](int it) { return it * 2.0f; }; | |
std::vector<int> tmp; | |
// filter | |
std::copy_if(std::begin(input), std::end(input), std::back_inserter(tmp), even); | |
// map | |
std::transform(std::begin(tmp), std::end(tmp), std::back_inserter(output), square); | |
print(output); | |
} | |
#endif | |
// after C++14 | |
#if __cplusplus >= 201402L | |
{ | |
std::vector<int> input = {1, 2, 3, 4, 5, 6}; | |
std::vector<double> output; /* {4, 8, 12} */ | |
auto even = [](auto it) { return it % 2 == 0; }; | |
auto square = [](auto it) { return it * 2.0f; }; | |
std::vector<int> tmp; | |
// filter | |
std::copy_if(std::begin(input), std::end(input), std::back_inserter(tmp), even); | |
// map | |
std::transform(std::begin(tmp), std::end(tmp), std::back_inserter(output), square); | |
print(output); | |
} | |
#endif | |
// after C++20 | |
#if __cplusplus >= 202002L | |
// https://en.cppreference.com/w/cpp/ranges | |
{ | |
std::vector<int> input = {1, 2, 3, 4, 5, 6}; | |
std::vector<double> output; | |
auto even = [](auto it) { return it % 2 == 0; }; | |
auto square = [](auto it) { return it * 2.0f; }; | |
auto tmp = input | std::views::filter(even) | std::views::transform(square); | |
std::ranges::copy(tmp, std::back_inserter(output)); | |
print(output); | |
} | |
#endif | |
// range-v3 | |
#ifdef USE_RANGE_V3 | |
// https://github.com/ericniebler/range-v3 | |
{ | |
std::vector<int> input = {1, 2, 3, 4, 5, 6}; | |
auto even = [](auto it) { return it % 2 == 0; }; | |
auto square = [](auto it) { return it * 2.0f; }; | |
auto output = input | ranges::views::filter(even) | ranges::views::transform(square) | ranges::to<std::vector<double>>; | |
print(output); | |
} | |
#endif | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment