티스토리 뷰

컴퓨팅/프로그래밍

람다함수

에어버스 2018. 6. 7. 16:05

람다함수

C++ 11.0 부터 적용

1. 람다함수를 직접 실행됨.

1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <iostream>
 
int main()
{
    [] { std::cout << "Hello World" << std::endl; }();
 
    std::cout << "Press any key...";
    _gettchar();
    return 0;
}
cs

 

<실행결과>

2. 함수 호출 방식

1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <iostream>
 
int main()
{
    auto func = [] { std::cout << "Hello World" << std::endl; };
    func();
    std::cout << "Press any key...";
    _gettchar();
    return 0;
}
cs


함수호출 방식으로 실행결과는 위와 같음.
자료형 auto는 자동으로 지정된다. 6번 람다함수에서 리턴값이 없으므로 void형으로 지정된다.

3. 람다함수에 인수 전달

1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include <iostream>
 
int main()
{
    auto func = [](int n) { std::cout << "Hello World : " << n << std::endl; };
    func(555);
    std::cout << "Press any key...";
    _gettchar();
    return 0;
}
cs

<실행결과>

4. 반환값을 가지는 경우

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include <iostream>
 
int main()
{
    auto func1 = [] { return 3.14; };
    auto func2 = [] (float f) { return f; };
    auto func3 = []() -> float { return 3.1415; };
    std::cout << func1() << "\n" << func2(3.141<< "\n" << func3() << std::endl;
    std::cout << "Press any key...";
    _gettchar();
    return 0;
}
cs

<실행결>

http://blog.naver.com/PostView.nhn?blogId=psychoria&logNo=220398922456

http://blog.naver.com/PostView.nhn?blogId=drvoss&logNo=220340563782

https://msdn.microsoft.com/ko-kr/library/jj987789.aspx

https://m.kdb.or.kr/info/info_04_view.html?field=title&keyword=REST&type=techreport&page=1&dbnum=173230&mode=detail&type=techreport

https://m.kdb.or.kr/info/info_04_view.html?field=&keyword=&type=techreport&page=44&dbnum=173993&mode=detail&type=techreport

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30