C++ lambda test code

#include <algorithm>#include <iostream> #include <functional> #include <vector> #include <string.h>using namespace std; void get_string_value(void *str_value) {vector<int32_t> param(15, 99);string _str_value;auto fill_string_data = [&]() {for (int i = 0; i < 15; i++) {_str_value += to_string(param[i]);if (i < param.size() - 1) {_str_value += ",";}}};fill_string_data();strncpy((char *)str_value, _str_value.c_str(), 1024);}int main() {     int a = 5;    int b = 8;    auto lambda1 = []{ cout << "Hello world\n"; };    std::function<void(int &a, int &b)> add = [a, b]() -> std::function<void(int &a, int &b)> {        cout << "a + b = " << (a + b) << endl;        return 0;    }();    cout << "a = " << a << endl;    cout << "b = " << b << endl;    lambda1();    vector<int> v{1, 2, 3, 4, 5, 6};    int sum = 0;    std::for_each(v.begin(), v.end(), [&sum] (const int& i) { sum += i; });    cout << "sum = " << sum << endl;    int sum2 = [](int x, int y){return (x + y);}(100, 101);    cout << "sum2 = " << sum2 << endl;char value[1024];get_string_value(value);    cout << "string values = " << value << endl;    return 0; } 

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章