C++ range分析 - 1
前言
追求simpler,cleaner,more expressive的C++在range表达被引入标准库之后,肯定会被大规模被使用。这里先来体验一下range的优美。
这边分析的代码都来之:https://github.com/ericniebler/range-v3.
ranges::for_each
#include <iostream>
#include <range/v3/all.hpp> // get everything
#include <string>
using std::cout;
int
main()
{
std::string s{"hello"};
// output: h e l l o
ranges::for_each(s, [](char c) { cout << c << ' '; });
cout << '\n';
}
输出:h e l l o
for_each
下面是for_each的代码,我们一部分一部分来分析:
include\range\v3\algorithm\for_each.hpp
struct for_each_fn
{
template<typename I, typename S, typename F, typename P = identity>
auto operator()(I begin, S end, F fun, P proj = P{}) cons