검색결과 리스트
2012/10에 해당되는 글 5건
- 2012.10.30 bind, function 사용법 - tr1도 같이...
- 2012.10.18 boost - any
- 2012.10.16 boost - thread_group
- 2012.10.16 boost - lexical_cast
- 2012.10.15 boost - multi_index
bind, function 간단 사용 방법
boost, tr1 다 설명...
// bind 할 함수 int sum( int a , int b ) { return a+b; } // boost bind 사용 // _1, _2 는 입력 인자 #include <boost/bind.hpp> boost::bind( &sum, _1, _2 ); // tr1 bind 사용 // boost와는 다르게 인자를 쓸려면 std::tr1::placeholders 사용 해야함. #include <functional> std::tr1::bind( &sum, std::tr1::placeholders::_1, std::tr1::placeholders::_2 ); /////////////////////////////////////////////////////////////////////////////////////////////// // bind 한 함수를 담는 boost::function #include <boost/function.hpp> boost::function< int(int, int) > temp = boost::bind( &sum, _1, _2 ); // bind 한 함수를 담는 std::tr1::function #include <functional> std::tr1::function< int(int, int) > temp = std::tr1::bind( &sum, std::tr1::placeholders::_1, std::tr1::placeholders::_2 ); /////////////////////////////////////////////////////////////////////////////////////////////// // bind class 처리 - tr1 도 똑같아용.... class boostBind { private: std::vector< boost::function< int( int, int ) > > m_funcArray; // 바인딩 한 함수를 모으기 위한 vector public: boostBind() { m_funcArray.push_back( boost::bind( &boostBind::sum, this, _1, _2 ) ); m_funcArray.push_back( boost::bind( &boostBind::sub, this, _1, _2 ) ); } ~boostBind() {} int sum( int a , int b ) { return a+b; } int sub( int a , int b ) { return a-b; } void run() { // 덧셈 함수 호출 int temp1 = m_funcArray[0]( 100, 200 ); // 뺄셈 함수 호출 int temp2 = m_funcArray[1]( 200, 100 ); } };
어떤 자료형이든 다 넣을수 있는 그런 컨테이너...
사용법은 아래와 같다...
그런데 이건 어디다 써먹으면 좋을까? 잘모르겠지만 일단 정리...
#include <boost/any.hpp> int main() { boost::any all; // 아래와 같이 넣으면 int형으로 변경 // 값만 써도 알아서 인식하지만 확실하게 어떤 자료형이 들어갔는지 알기 위해 all = (int)100; // 이상태에서는 자료형은 안바뀌고 값만 갱신 all = 200; // 여기서 다른 자료형으로 데이터를 넣으면 자료형도 변경 되면서 데이터 갱신 all = (float)1.3f; // 값을 꺼내는 방법 첫번째 - 타입을 비교 하고 맞을때 값 꺼내기 if( all.type() == typeid(float) ) { float &output = boost::any_cast<float &>(all); } // 값을 꺼내는 방법 두번째 - try catch 문을 사용 해서 꺼내기 // 여기서는 실패에서 에러 나겠죵... try { int &otput = boost::any_cast<int &>(all); } catch(const boost::bad_any_cast &e) { printf("%s\n", e.what() ); } // 변수 초기화는 요렇게??? all = boost::any(); if( all.empty() ) { printf("아무것도 없네요\n"); } return 0; }
이거 쓰면 Thread를 그룹으로 묶어서 쓸수 있심..
사용법은 아래 보셈..
#include "boost/thread.hpp" class threadFunc { int m_a; public: threadFunc( int a ) { m_a = a; } void operator()() { printf("[%d]일단 들어왔네!!! [%d]\n", boost::this_thread::get_id(), m_a ); Sleep(5000); printf("[%d] 끝났네 [%d]\n", boost::this_thread::get_id(), m_a ); } }; int main() { boost::thread_group tg; tg.create_thread( threadFunc(1) ); // 1번 스래드 생성 tg.add_thread(new boost::thread( threadFunc(2) ) ); // 2번 스래드 생성 tg.add_thread(new boost::thread( threadFunc(3) ) ); // 3번 스래드 생성 //모든 스래드가 종료 될때까지 대기 tg.join_all(); return 0; }
문자를 숫자로 or 숫자를 문자로 바꿀때 사용 한다.
사용법은 간단하다.. 아래보셈..
#include "boost/lexical_cast.hpp" int main() { std::string input = "1"; int output = 0; try { convertDta = boost::lexical_cast<int>( inputValue ); } catch(boost::bad_lexical_cast &e) { // 변경이 안되면 아래 에러 출력 printf("%s\n", e.what() ); return false; } return 0; }
그런데 이거 잘 써먹지는 못할듯..
문제가 2가지가 있심.. BYTE(unsigned char) 변환이 잘 안된다는거 하고
atoi(itoa), stringstream 등으로 변환하는거보단 촐랭 느리다는거...
아무튼 오늘도 뭔가 하나 올림..
boost의 mulit_index를 알기 쉽게 설명해준 자료가 있어 링크합니다..
(흥배님 감사합니다!!)
개념 설명은 위에것과 아래 첨부된 [SDC 3rd] 01 Boost_multiindex(2012-05-19).zip 파일을 보면 될것입니다.
composite_key 사용 설명은 따로 없어서 내가 추가로 정리 해서 boostTest._multi_index.zip 파일 올립니다.
[SDC 3rd] 01 Boost_multiindex(2012-05-19).zip
RECENT COMMENT