티스토리 뷰

IT지식

c++ 람다식 예

민자르 2018. 4. 17. 14:02
반응형

예제 1

람다 식을 입력했기 때문에 다음과 같이 auto 변수 또는 함수 개체에 할당할 수 있습니다.

// declaring_lambda_expressions1.cpp  
// compile with: /EHsc /W4  
#include <functional>  
#include <iostream>  
  
int main()  
{  
  
    using namespace std;  
  
    // Assign the lambda expression that adds two numbers to an auto variable.  
    auto f1 = [](int x, int y) { return x + y; };  
  
    cout << f1(2, 3) << endl;  
  
    // Assign the same lambda expression to a function object.  
    function<int(int, int)> f2 = [](int x, int y) { return x + y; };  
  
    cout << f2(3, 4) << endl;  
}  


출력

5
7

설명

자세한 내용은 autofunction 클래스 및 함수 호출을 참조하세요.

람다 식은 함수의 본문에서 대부분 선언되지만 변수를 초기화할 수 있는 어느 곳에서나 선언할 수 있습니다.

예제 2

식을 호출할 때 대신 식을 선언할 때 Visual C++ 컴파일러는 캡처된 변수에 람다 식을 바인딩합니다. 다음 예제에서는 변수 지역 변수 i값과 참조로서 변수 j를 캡처하는 람다 식을 보여 줍니다. 람다 식은 i를 값으로 캡처하기 때문에 프로그램에서 i 이상을 다시 할당하면 식의 결과에 영향을 주지 않습니다. 그러나 람다 식을 j를 참조로 캡처하기 때문에 j를 다시 할당하면 식의 결과에 영향을 주지 않습니다.

// declaring_lambda_expressions2.cpp  
// compile with: /EHsc /W4  
#include <functional>  
#include <iostream>  
  
int main()  
{  
   using namespace std;  
  
   int i = 3;  
   int j = 5;  
  
   // The following lambda expression captures i by value and  
   // j by reference.  
   function<int (void)> f = [i, &j] { return i + j; };  
  
   // Change the values of i and j.  
   i = 22;  
   j = 44;  
  
   // Call f and print its result.  
   cout << f() << endl;  
}  


출력

47

다음 코드 조각과 같이 람다 식을 즉시 호출할 수 있습니다. 두 번째 조각은 람다를 find_if 등의 STL(표준 템플릿 라이브러리) 알고리즘에 대한 인수로 전달하는 방법을 보여 줍니다.

예제 1

이 예제에서는 두 정수의 합을 반환하고 식 인수를 사용하여 인수 5 및 4로 식을 즉시 호출하는 람다 식을 선언합니다.

// calling_lambda_expressions1.cpp  
// compile with: /EHsc  
#include <iostream>  
  
int main()  
{  
   using namespace std;  
   int n = [] (int x, int y) { return x + y; }(5, 4);  
   cout << n << endl;  
}


출력

9

예제 2

이 예제에서는 람다 식을 find_if 함수에 대한 인수로 전달합니다. 람다 식은 매개 변수가 짝수이면 true를 반환합니다.

// calling_lambda_expressions2.cpp  
// compile with: /EHsc /W4  
#include <list>  
#include <algorithm>  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
  
    // Create a list of integers with a few initial elements.  
    list<int> numbers;  
    numbers.push_back(13);  
    numbers.push_back(17);  
    numbers.push_back(42);  
    numbers.push_back(46);  
    numbers.push_back(99);  
  
    // Use the find_if function and a lambda expression to find the   
    // first even number in the list.  
    const list<int>::const_iterator result =   
        find_if(numbers.begin(), numbers.end(),[](int n) { return (n % 2) == 0; });  
  
    // Print the result.  
    if (result != numbers.end()) {  
        cout << "The first even number in the list is " << *result << "." << endl;  
    } else {  
        cout << "The list contains no even numbers." << endl;  
    }  
}


출력

목록의 첫 번째 짝수는 42입니다.

설명

find_if 함수에 대한 자세한 내용은 find_if를 참조하세요. 공용 알고리즘을 수행하는 STL 함수에 대한 자세한 내용은 <algorithm>을 참조하세요.

예제

이 예제와 같이 람다 식을 다른 람다 식 안에 중첩할 수 있습니다. 안쪽 람다 식은 인수를 2를 곱한 후 결과를 반환합니다. 바깥쪽 람다 식은 안쪽 람다 식의 인수와 함께 호출하고 결과에 3을 더합니다.

// nesting_lambda_expressions.cpp  
// compile with: /EHsc /W4  
#include <iostream>  
  
int main()  
{  
    using namespace std;  
  
    // The following lambda expression contains a nested lambda  
    // expression.  
    int timestwoplusthree = [](int x) { return [](int y) { return y * 2; }(x) + 3; }(5);  
  
    // Print the result.  
    cout << timestwoplusthree << endl;  
}


출력

13

설명

이 예제에서 [](int y) { return y * 2; }는 중첩된 람다 식입니다.

예제

대부분의 프로그래밍 언어는 고차 함수의 개념을 지원합니다. 고차 함수는 람다 식으로, 다른 람다 식을 인수로 취하거나 람다 식을 반환합니다. 함수클래스를 사용하여 C++ 람다 식이 고차 함수와 같이 동작하도록 할 수 있습니다. 다음 예제에서는 function 개체를 반환하는 람다 식과 인수로서 function 개체를 취하는 람다 식을 보여 줍니다.

// higher_order_lambda_expression.cpp  
// compile with: /EHsc /W4  
#include <iostream>  
#include <functional>  
  
int main()  
{  
    using namespace std;  
  
    // The following code declares a lambda expression that returns   
    // another lambda expression that adds two numbers.   
    // The returned lambda expression captures parameter x by value.  
    auto addtwointegers = [](int x) -> function<int(int)> {   
        return [=](int y) { return x + y; };   
    };  
  
    // The following code declares a lambda expression that takes another  
    // lambda expression as its argument.  
    // The lambda expression applies the argument z to the function f  
    // and multiplies by 2.  
    auto higherorder = [](const function<int(int)>& f, int z) {   
        return f(z) * 2;   
    };  
  
    // Call the lambda expression that is bound to higherorder.   
    auto answer = higherorder(addtwointegers(7), 8);  
  
    // Print the result, which is (7+8)*2.  
    cout << answer << endl;  
}


출력

30

예제

함수의 본문에서 람다 식을 사용할 수 있습니다. 람다 식은 바깥쪽 함수에서 액세스할 수 있는 모든 함수 또는 데이터 멤버에 액세스할 수 있습니다. this 포인터를 명시적으로나 암시적으로 캡처하여 바깥쪽 클래스의 데이터 멤버 및 함수에 대한 액세스를 제공할 수 있습니다.

다음과 같이 함수에서 명시적으로 this 포인터를 사용할 수 있습니다.

void ApplyScale(const vector<int>& v) const  
{  
   for_each(v.begin(), v.end(),   
      [this](int n) { cout << n * _scale << endl; });  
}


void ApplyScale(const vector<int>& v) const  
{  
   for_each(v.begin(), v.end(),   
      [=](int n) { cout << n * _scale << endl; });  
}


다음 예제에서는 소수 자릿수 값을 캡슐화하는 Scale 클래스를 보여 줍니다.


// function_lambda_expression.cpp  
// compile with: /EHsc /W4  
#include <algorithm>  
#include <iostream>  
#include <vector>  
  
using namespace std;  
  
class Scale  
{  
public:  
    // The constructor.  
    explicit Scale(int scale) : _scale(scale) {}  
  
    // Prints the product of each element in a vector object   
    // and the scale value to the console.  
    void ApplyScale(const vector<int>& v) const  
    {  
        for_each(v.begin(), v.end(), [=](int n) { cout << n * _scale << endl; });  
    }  
  
private:  
    int _scale;  
};  
  
int main()  
{  
    vector<int> values;  
    values.push_back(1);  
    values.push_back(2);  
    values.push_back(3);  
    values.push_back(4);  
  
    // Create a Scale object that scales elements by 3 and apply  
    // it to the vector object. Does not modify the vector.  
    Scale s(3);  
    s.ApplyScale(values);  
}  


출력

3
6
10
12

설명

ApplyScale 함수는 람다 식을 사용하여 vector 개체에서 스케일 값 및 각 요소의 곱을 인쇄합니다. 람다 식은 포인터를 this 멤버에 액세스할 수 있도록 암시적으로 _scale를 캡처합니다.

예제

람다 식이 형식화되기 때문에 C++ 템플릿과 함께 사용할 수 있습니다. 다음 예제에서는 negate_all 및 print_all 함수를 보여 줍니다. negate_all 함수는 단항 operator-를 vector 개체의 각 요소에 적용합니다. print_all 함수는 vector 개체의 각 요소를 콘솔에 인쇄합니다.

// template_lambda_expression.cpp  
// compile with: /EHsc  
#include <vector>  
#include <algorithm>  
#include <iostream>  
  
using namespace std;  
  
// Negates each element in the vector object. Assumes signed data type.  
template <typename T>  
void negate_all(vector<T>& v)  
{  
    for_each(v.begin(), v.end(), [](T& n) { n = -n; });  
}  
  
// Prints to the console each element in the vector object.  
template <typename T>  
void print_all(const vector<T>& v)  
{  
    for_each(v.begin(), v.end(), [](const T& n) { cout << n << endl; });  
}  
  
int main()  
{  
    // Create a vector of signed integers with a few elements.  
    vector<int> v;  
    v.push_back(34);  
    v.push_back(-43);  
    v.push_back(56);  
  
    print_all(v);  
    negate_all(v);  
    cout << "After negate_all():" << endl;  
    print_all(v);  
}


출력

34
-43
56
negate_all() 이후:
-34
43
-56

예제

람다 수식의 본문은 SEH(구조적 예외 처리)와 C++ 예외 처리에 대한 규칙을 따릅니다. 람다 식의 본문에는 양각된 예외를 처리하거나 예외 처리를 포함하는 범위를 지연시킬 수 있습니다. 다음 예제에서는 for_each 함수와 람다 식을 사용하여 하나의 vector 개체에 다른 개체의 값을 채웁니다. try/catch 블록을 사용하여 첫 번째 벡터에 대한 잘못된 액세스를 처리합니다.

// eh_lambda_expression.cpp  
// compile with: /EHsc /W4  
#include <vector>  
#include <algorithm>  
#include <iostream>  
using namespace std;  
  
int main()  
{  
    // Create a vector that contains 3 elements.  
    vector<int> elements(3);  
  
    // Create another vector that contains index values.  
    vector<int> indices(3);  
    indices[0] = 0;  
    indices[1] = -1; // This is not a valid subscript. It will trigger an exception.  
    indices[2] = 2;  
  
    // Use the values from the vector of index values to   
    // fill the elements vector. This example uses a   
    // try/catch block to handle invalid access to the   
    // elements vector.  
    try  
    {  
        for_each(indices.begin(), indices.end(), [&](int index) {   
            elements.at(index) = index;   
        });  
    }  
    catch (const out_of_range& e)  
    {  
        cerr << "Caught '" << e.what() << "'." << endl;  
    };  
}


출력

Caught 'invalid vector<T> subscript'.


관리되는 형식이 있는 람다 식 사용(C++/CLI)

예제

람다 식의 캡처 절에는 관리되는 형식의 변수가 포함될 수 없습니다. 그러나 관리되는 형식이 포함된 인수를 람다 식의 매개 변수 목록으로 전달할 수 있습니다. 다음 예제에서는 관리되지 않는 지역 변수 ch를 캡처하는 람다 식을 포함하고 매개 변수로서 System.String 개체를 가져옵니다.

// managed_lambda_expression.cpp  
// compile with: /clr  
using namespace System;  
  
int main()  
{  
    char ch = '!'; // a local unmanaged variable  
  
    // The following lambda expression captures local variables  
    // by value and takes a managed String object as its parameter.  
    [=](String ^s) {   
        Console::WriteLine(s + Convert::ToChar(ch));   
    }("Hello");  
}


출력

Hello!

STL/CLR 라이브러리에서 람다 식을 사용할 수도 있습니다.

728x90
반응형
댓글