Qt_GUI_BG_3_1 - 8BitsCoding/RobotMentor GitHub Wiki

๋žŒ๋‹ค์‹์˜ ๊ธฐ๋ณธ๊ตฌ์กฐ

#include <iostream>

using namespace std;

int main()
{
	/* 
	๋žŒ๋‹ค์˜ ๊ธฐ๋ณธ์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
	[capture list] (parameter list) {function body}
	*/

	// Give lambda function a name and call it
	auto func = []() {
		cout << "Hello World!" << endl;
	};

	func();
	

	// Call lambda function directly after definition
	[]() {
		cout << "Hello World!" << endl;
	}();	// ์‹œ์ž‘๊ณผ ๋™์‹œ์— ๋™์ž‘
	

	// Define a lambda function that takes parameters
	[](int a, int b) {
		cout << " a + b  = " << a + b << endl;
	}(7,3);
	

	
	// Define a lambda that returns something
	int sum  = [](int a, int b)->int{
		cout << " a + b  = " << a + b << endl;
		return a + b;
	}(7, 3);

	cout << "The sum is : " << sum << endl;
	

	
	// Capture Lists
	int a = 7;
	int b = 3;
	[a,b]() {
		cout << "a is : " << a << endl;
		cout << "b is : " << b << endl;
	}();
	

	// Caturing by value & reference
	int c = 42;
	// auto func = [c]() {
	auto func = [&c]() {
		cout << "The value of c is : " << c << endl;
	};

	for (int i = 1; i < 5; i++) {
		cout << "The outer value of c is : " << c << endl;
		func();
		c++;
	}

    // Caturing everything by value & reference
	int c = 42;
	//auto func = [=]() {
	auto func = [&]() {
		cout << "The value of c is : " << c << endl;
	};

	for (int i = 1; i < 5; i++) {
		cout << "The outer value of c is : " << c << endl;
		func();
		c++;
	}

	return 0;
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ