#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
namespace std {
namespace tr1 = boost;
}
// 테스트용 클래스
class CInt
{
public:
CInt(int n = 0)
{
n_ = n;
}
CInt(const CInt &t)
{
n_ = t.n_;
}
const CInt& operator=(const CInt &t)
{
if (this != &t) {
n_ = t.n_;
}
return *this;
}
const CInt& operator=(int n)
{
n_ = n;
return *this;
}
~CInt() {}
public:
void Print(string str) const
{
cout << n_ << str;
}
int Get() const
{
cout << n_;
return n_;
}
operator int() const
{
return Get();
}
private:
int n_;
};
void Write(int n, string str)
{
cout << n << str;
}
int main()
{
std::vector<int> v;
std::vector<CInt> vc;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
vc.push_back(i);
}
std::random_shuffle(v.begin(), v.end());
std::random_shuffle(vc.begin(), vc.end());
string sep = "-";
// 기존 C++ 바인딩
for_each(v.begin(), v.end(), bind2nd(ptr_fun(Write), sep));
cout << endl;
// 새로운 C++ 바인딩
for_each(v.begin(), v.end(), tr1::bind(Write, ::_1, sep));
cout << endl;
using boost::lambda::_1;
// lambda 표현식
for_each(v.begin(), v.end(), cout << _1 << sep );
cout << endl;
// 기존 C++ 바인딩
for_each(vc.begin(), vc.end(), bind2nd(mem_fun_ref(&CInt::Print), sep));
cout << endl;
// 새로운 C++ 바인딩
for_each(vc.begin(), vc.end(), tr1::bind(tr1::mem_fn(&CInt::Print), ::_1, sep));
cout << endl;
}
'Dev > C++' 카테고리의 다른 글
A member template cannot be virtual (0) | 2007.05.14 |
---|---|
sgi STL - Why does Bounds Checker™ say that I have memory leaks? (0) | 2007.05.14 |
MinGW - Why is my C++ binary so large? (0) | 2007.05.14 |
Why doesn't C++ provide a "finally" construct? (0) | 2007.05.14 |
loop, 템플릿 메타프로그래밍 버전.. (0) | 2007.05.13 |