C++ Set: Using Lambda Functions for Iteration
Here's an example of how to use a lambda function with a C++ set:
#include <iostream>
#include <set>
int main() {
std::set<int> myset;
// Insert values into the set
for (int i = 0; i < 10; i++) {
myset.insert(i);
}
// Use a lambda function to print each value in the set
std::for_each(myset.begin(), myset.end(), [](int value) {
std::cout << value << ' ';
});
return 0;
}
In this example, we create a set of integers and insert values into it using a for loop. We then use the std::for_each algorithm to iterate over the set and print each value using a lambda function. The lambda function takes an integer value as its argument and simply prints it to the console.
原文地址: https://www.cveoy.top/t/topic/ogPL 著作权归作者所有。请勿转载和采集!