新版本的变更 / C++14

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

// *** before: ***
// the type of a lambda parameter had to be written out, so a second
// lambda was needed for every new type
void oldWay() {
    std::vector<int> numbers{312};
    std::vector<std::string> words{"pear""fig""plum"};

    std::sort(numbers.begin(), numbers.end(),
              [](int a, int b) { return a > b; });
    std::sort(words.begin(), words.end(),
              [](const std::string& a, const std::string& b) { return a > b; });

    std::cout << numbers[0] << " " << words[0] << "\n";
}

// *** in version C++14: ***
void newWay() {
    auto descending = [](const auto& a, const auto& b) { return a > b; };

    std::vector<int> numbers{312};
    std::vector<std::string> words{"pear""fig""plum"};

    std::sort(numbers.begin(), numbers.end(), descending);   // one lambda
    std::sort(words.begin(), words.end(), descending);       // for both types

    auto twice = [](auto value) { return value + value; };
    std::cout << twice(21) << " " << twice(std::string("ab")) << "\n";
    std::cout << numbers[0] << " " << words[0] << "\n";
}

int main() {
    oldWay();
    newWay();
}