Native/C++
c++0x lamda
aucd29
2013. 10. 2. 19:06
http://vsts2010.tistory.com/75
람다 사용 방법은 아래와 같습니다.
[code]
[](파라메터) { 식 }
[](int x) -> int { return x + 50; }
[/code]
아래의 클래스는 test 클래스에 res 가 true 인 것을 찾기 위한 과정에 일부이고 이를 위해서 일반적으로 사용해온 방식과 람다의 에 방식을 비교하기 위해서 생성한 코드 입니다.
핵심적으로 봐야할 부분은 find_it 에서 별도에 struct 를 구성하여 만들었던 부분이 람다구조로 손쉽게 구현되는 걸 볼 수 있습니다.
이러한 방법에 대한 개략적인 소개는 모어 조엘원 소프트웨어 라는 책에 javascript 기반으로 잘 설명되어 있습니다.
[code]
// 1. Test object 에 reference 로 t를 받게 됨
// 2. 리턴 타입 bool 형태를 가짐
// 3. 1에서 받은 t에 getRes 값이 true 와 비교해 참일 경우 true 를 반환한다.
[](Test& t) -> bool { return true == t.getRes() }
[/code]
[code]
#include <stdio.h>
class Test
{
public:
Test(int index)
: _res(false)
, _index(index)
{
}
void setRes(bool res)
{
_res = res;
}
bool getRes()
{
return _res;
}
int getIndex()
{
return _index;
}
private:
bool _res;
int _index;
};
// ------------------------------------------------------------
struct findTrueRes
{
bool operator() (Test& t) const { return t.getRes(); }
};
// normal
int main(int argc, char *argv[])
{
// 객체 생성
Test t1(1);
Test t2(2);
Test t3(3);
// 특정 위치에 res를 true 로 설정
t2.setRes(true);
// 백터에 입력
vector<Test> arrTest;
arrTest.push_back(t1);
arrTest.push_back(t2);
arrTest.push_back(t3);
// 나 잡아봐라?
vector<Test>::iterator it;
it = find_if(arrTest.begin(), arrTest.end(), findTrueRes);
if (it != arrTest.end())
{
printf("found true t index : %d\n", it->getIndex());
}
return 0;
}
// ------------------------------------------------------------
// lamda
int main(int argc, char *argv[])
{
Test t1(1);
Test t2(2);
Test t3(3);
t2.setRes(true);
vector<Test> arrTest;
arrTest.push_back(t1);
arrTest.push_back(t2);
arrTest.push_back(t3);
vector<Test>::iterator it;
it = find_if(arrTest.begin(), arrTest.end(), [](Test& t) -> bool { return true == t.getRes() });
if (it != arrTest.end())
{
printf("found true t index : %d\n", it->getIndex());
}
return 0;
}
[/code]
람다 사용 방법은 아래와 같습니다.
[code]
[](파라메터) { 식 }
[](int x) -> int { return x + 50; }
[/code]
아래의 클래스는 test 클래스에 res 가 true 인 것을 찾기 위한 과정에 일부이고 이를 위해서 일반적으로 사용해온 방식과 람다의 에 방식을 비교하기 위해서 생성한 코드 입니다.
핵심적으로 봐야할 부분은 find_it 에서 별도에 struct 를 구성하여 만들었던 부분이 람다구조로 손쉽게 구현되는 걸 볼 수 있습니다.
이러한 방법에 대한 개략적인 소개는 모어 조엘원 소프트웨어 라는 책에 javascript 기반으로 잘 설명되어 있습니다.
[code]
// 1. Test object 에 reference 로 t를 받게 됨
// 2. 리턴 타입 bool 형태를 가짐
// 3. 1에서 받은 t에 getRes 값이 true 와 비교해 참일 경우 true 를 반환한다.
[](Test& t) -> bool { return true == t.getRes() }
[/code]
[code]
#include <stdio.h>
class Test
{
public:
Test(int index)
: _res(false)
, _index(index)
{
}
void setRes(bool res)
{
_res = res;
}
bool getRes()
{
return _res;
}
int getIndex()
{
return _index;
}
private:
bool _res;
int _index;
};
// ------------------------------------------------------------
struct findTrueRes
{
bool operator() (Test& t) const { return t.getRes(); }
};
// normal
int main(int argc, char *argv[])
{
// 객체 생성
Test t1(1);
Test t2(2);
Test t3(3);
// 특정 위치에 res를 true 로 설정
t2.setRes(true);
// 백터에 입력
vector<Test> arrTest;
arrTest.push_back(t1);
arrTest.push_back(t2);
arrTest.push_back(t3);
// 나 잡아봐라?
vector<Test>::iterator it;
it = find_if(arrTest.begin(), arrTest.end(), findTrueRes);
if (it != arrTest.end())
{
printf("found true t index : %d\n", it->getIndex());
}
return 0;
}
// ------------------------------------------------------------
// lamda
int main(int argc, char *argv[])
{
Test t1(1);
Test t2(2);
Test t3(3);
t2.setRes(true);
vector<Test> arrTest;
arrTest.push_back(t1);
arrTest.push_back(t2);
arrTest.push_back(t3);
vector<Test>::iterator it;
it = find_if(arrTest.begin(), arrTest.end(), [](Test& t) -> bool { return true == t.getRes() });
if (it != arrTest.end())
{
printf("found true t index : %d\n", it->getIndex());
}
return 0;
}
[/code]