728x90
여러개의 변수를 주고 받거나 동시에 다뤄야할 때 유용한-
대표적으로 변수는 하나 넘겨줘야되는데 많은 양의 정보를 넘겨줄 때 유용 할 것 같네요
예제 소스
#include "stdafx.h"
#include <REGEX>
#include <TUPLE>
#include <BOOST/tuple.hpp/tuple>
#include <BOOST/function.hpp>
#include <TYPEINFO>
#include <IOSTREAM>
class CTest
{
int m_nVal;
public:
CTest() : m_nVal(0) {}
CTest(int n) : m_nVal(n) {}
};
// 생성 및 초기화
boost::tuple<> t0;
boost::tuple<INT> t1;
boost::tuple<INT> t2(3); // 3으로 초기화
boost::tuple<CTEST> t3;
boost::tuple<CTEST> t4(CTest(1)); // 1로 초기화
boost::tuple<INT> t5(t2); // 3 으로 초기화
boost::tuple<FLOAT> t6(t2); // 3.0 으로 초기화
boost::tuple<CTEST, int> t7(1, 2); // 1, 2 로 초기화
void foo(boost::tuple<> val)
{
}
int _tmain(int argc, _TCHAR* argv[])
{
// 함수 내에서의 초기화 및 변수 적용
{
int i = 2011;
int& j = i;
boost::tuple<INT, float> t1(boost::make_tuple(1, 3.14f));
boost::tuple<INT, int> t2(boost::make_tuple(i, j));
t1 = t2;
std::cout << "---------------" << std::endl;
}
// 응용
{
int i = 2011;
int j = 11;
boost::make_tuple(boost::ref(i), boost::cref(j));
// return tuple<INT&, int& const> i에 대한 참조, j에 대한 상수 참조
std::cout << "---------------" << std::endl;
}
// 여러개의 외부 변수를 동시에 바꾸기
{
int i = 1;
int j = 2;
int k = 3;
std::cout << i << ", " << j << ", " << k << std::endl; // output 1, 2, 3
boost::tie(i, boost::tuples::ignore, k) = boost::make_tuple(5, 6, 7);
std::cout << i << ", " << j << ", " << k << std::endl; // output 5, 2, 7
std::cout << "---------------" << std::endl;
}
// element 참조
{
boost::tuples::tuple<INT, float int,> tt0(2011, 11, 3.14f);
std::cout << boost::tuples::get<0>(tt0) << ", " << boost::tuples::get<1>(tt0) << ", " << boost::tuples::get<2>(tt0) << std::endl;
// output 2011, 11, 3.14f
boost::tuples::get<0>(tt0) = 1;
boost::tuples::get<1>(tt0) = 2;
boost::tuples::get<2>(tt0) = 3;
std::cout << boost::tuples::get<0>(tt0) << ", " << boost::tuples::get<1>(tt0) << ", " << boost::tuples::get<2>(tt0) << std::endl;
// output 1, 2, 3
std::cout << "---------------" << std::endl;
}
int n;
std::cin >> n;
return 0;
}
728x90
'프로그래밍 > boost' 카테고리의 다른 글
| boost::program_options (0) | 2012.11.16 |
|---|---|
| boost::function / boost::tuple 의 활용 (0) | 2012.11.07 |
| boost::shared_ptr 과 Virtual Function 과 boost::timer (0) | 2012.10.25 |
| boost::random (0) | 2012.10.24 |
| boost::asio 비동기 서버/클라 step 2 (4) | 2012.09.25 |
댓글