Home > Backend Development > C++ > What are some practical examples of using C++ lambda expressions for data filtering and transformation?

What are some practical examples of using C++ lambda expressions for data filtering and transformation?

王林
Release: 2024-04-18 09:15:01
Original
817 people have browsed it

In C, lambda expressions can be used to easily filter and transform data. For example, you can use lambda expressions to filter odd elements in a container, transform elements in a container, filter and transform associative containers, use lambda expressions in algorithms, and pass lambda expressions as function arguments. These methods can make data processing tasks simpler and more efficient.

使用 C++ lambda 表达式进行数据过滤和转换有哪些实战案例?

A practical case of using C lambda expressions for data filtering and transformation

Lambda expressions are very useful in C, especially It's when filtering and transforming data. Here are some examples of practical applications:

1. Filter elements in a container

std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};

// 使用 lambda 表达式过滤奇数
std::vector<int> odd_numbers = std::filter(numbers.begin(), numbers.end(), [](int n) { return n % 2 != 0; });
Copy after login

2. Transform elements in a container

std::vector<double> squares = std::transform(numbers.begin(), numbers.end(), [](int n) { return n * n; });
Copy after login

3. Filter and transform associated containers

std::map<int, std::string> name_age = {
    {1, "John"},
    {2, "Mary"},
    {3, "Bob"},
    {4, "Alice"}
};

// 过滤年龄大于 2 的人
auto filtered_name_age = std::map<int, std::string>(name_age.begin(), name_age.end(), [](const std::pair<int, std::string>& p) { return p.second.size() > 2; });
Copy after login

4. Use lambda expressions in algorithms

std::sort(numbers.begin(), numbers.end(), [](int a, int b) { return a > b; }); // 从大到小排序
Copy after login

5. Passing lambda expressions as function parameters

void process_data(std::vector<int>& data, std::function<void(int)> callback) {
    for (int& element : data) {
        callback(element);
    }
}

process_data(numbers, [](int n) { std::cout << n << " "; }); // 打印出容器中的每个元素
Copy after login

These are just some of the ways in C to use lambda expressions for data filtering and transformation. By leveraging lambda expressions, you can write concise and powerful code to handle various data manipulation tasks.

The above is the detailed content of What are some practical examples of using C++ lambda expressions for data filtering and transformation?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template