简体   繁体   English

C ++中的模板lambda表达式

[英]template lambda expression in C++

I am upgrading to C++ 11 and have basic idea about lambda expression as mentioned in https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019 我正在升级到C ++ 11,并且具有关于lambda表达式的基本概念,如https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=vs-2019中所述

I have following doubts about the code below captured from http://project-thrill.org/ 我对以下从http://project-thrill.org/捕获的代码有疑问

The following program counts the number of occurrences of each unique word in a text 下面的程序计算文本中每个唯一单词的出现次数

void WordCount(thrill::Context& ctx, std::string input, std::string output) 
{
    using Pair = std::pair<std::string, size_t>;
    auto word_pairs = ReadLines(ctx, input)
                            .template FlatMap<Pair>(
                            // flatmap lambda: split and emit each word
                                [](const std::string& line, auto emit)
                                {
                                    Split(line, ’ ’, [&](std::string_view sv)
                                    {
                                        emit(Pair(sv.to_string(), 1));
                                    });
                                });
    word_pairs.ReduceByKey(
        // key extractor: the word string
        [](const Pair& p) { return p.first; },
        // commutative reduction: add counters
        [](const Pair& a, const Pair& b) 
        {
            return Pair(a.first, a.second + b.second);
        })
    .Map([](const Pair& p)
    {
        return p.first + ": " + std::to_string(p.second); 
    }).WriteLines(output);
}

first question what is .template FlatMap 第一个问题是.template FlatMap是什么

is FlatMap a template type lambda function which is operating on return of ReadLines ? FlatMap是在返回ReadLines运行的模板类型lambda函数吗?

inside FlatMap<Pair> how the value is geting passed to (const std::string& line, auto emit) and who passes the value ? FlatMap<Pair>内部,如何将值传递给(const std::string& line, auto emit)以及谁传递值?


inside ReduceByKey function, to the argument [](const Pair& p) of lambda function, how value is getting passed ? ReduceByKey函数内部, [](const Pair& p) lambda函数的参数[](const Pair& p) ,如何传递值?

what is .template FlatMap 什么是.template FlatMap

It's a syntactic necessity, to indicate that FlatMap is a template member, and that the < is part of an explicit template argument and not a comparison operator. 表示FlatMap是模板成员,并且<是显式模板参数的一部分,而不是比较运算符,这在语法上是必须的。

is FlatMap a template type lambda function which is operating on return of ReadLines ?` FlatMap是在返回ReadLines运行的模板类型lambda函数吗?

That's not a thing. 没关系 FlatMap is a template member function, and it receives a Callable as it's sole parameter. FlatMap是模板成员函数,它接收Callable作为其唯一参数。 Here it is called with a lambda expression. 在这里用lambda表达式进行调用。

inside FlatMap<Pair> how the value is geting passed to (const std::string& line, auto emit) and who passes the value ? FlatMap<Pair>内部,如何将值传递给(const std::string& line, auto emit)以及谁传递值?

The body of FlatMap . FlatMap的主体。 like how emit is called inside the lambda. 就像在lambda内如何emit Presumably it is called once for each line in the input, and generates multiple Pair as output from each input line. 大概为输入中的每一行调用一次,并从每条输入行生成多个对作为输出。

inside ReduceByKey function, to the argument [](const Pair& p) of lambda function, how value is getting passed ? ReduceByKey函数内部, [](const Pair& p) lambda函数的参数[](const Pair& p) ,如何传递值?

The same way. 以同样的方式。 ReduceByKey has two Callable arguments, and calls them based on the member's of word_pairs (which is the result of FlatMap ) ReduceByKey有两个Callable参数,并根据word_pairs成员的成员来调用它们(这是FlatMap的结果)

It will categorise each item it sees by the first function, and then combine within each category with the second function. 它将通过第一个功能对看到的每个项目进行分类,然后在每个类别中与第二个功能组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM