C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[492] vector 와 any 와 shared_ptr 의 만남 첫번째
nicekr.황경록 [mpbox] 8671 읽음    2005-08-12 20:22
^^

//---------------------------------------------------------------------------

#include <vcl.h>

#include <stdio.h>

#pragma hdrstop

#include <vector>
#include <queue>
#include <list>
#include <boost/any.hpp>
#include <boost/shared_ptr.hpp>

using boost::any_cast;
using boost::shared_ptr;

typedef std::vector<boost::any> many;

//---------------------------------------------------------------------------

class TZPacket
{
public:
    TZPacket() {};

    AnsiString sayHello() { return AnsiString("Hello!"); }
};

typedef boost::shared_ptr<TZPacket> ZPacket;
typedef boost::any ANY_PARAM;

#pragma argsused

many mylist;

void addElement( ANY_PARAM pElement )
{
    mylist.push_back( pElement );
}

ANY_PARAM getElementAt( int iIndex )
{
    if( mylist.size() <= iIndex )
    {
        #ifdef APP_DOUBT
        zout.trace("%s%s\n", APP_DOUBT, "TZNormalQueue::getElementAt | FList->Count <= iIndex");
        #endif

        return NULL;
    }

    return mylist[iIndex];
}

ANY_PARAM removeElementAt( int iIndex )
{
    ANY_PARAM p = mylist[iIndex];

    mylist.erase(&mylist.at(iIndex));

    return p;
}

int main(int argc, char* argv[])
{
    ZPacket a( new TZPacket() );

    addElement( a );
    printf("use count : %d\n", a.use_count());
    addElement( a );
    printf("use count : %d\n", a.use_count());
    addElement( a );
    printf("use count : %d\n", a.use_count());
    addElement( a );
    printf("use count : %d\n", a.use_count());   
    addElement( a );

    printf("vlist count = %d\n", mylist.size());

    for( int i = 0; i < mylist.size(); i ++ )
    {
        printf( "%s\n", any_cast<ZPacket>(getElementAt(i))->sayHello() );
    }

    printf("-----\n");

    ZPacket b = any_cast<ZPacket>(removeElementAt(0));

    printf("%s\n", a->sayHello());
    printf("%s\n", b->sayHello());

    while( mylist.size() )
    {
        ZPacket c = any_cast<ZPacket>(removeElementAt(0));

        printf("%s\n", c->sayHello());       
    }

    printf("vlist count = %d\n", mylist.size());

    printf("use count : %d\n", a.use_count());

    system("pause");

    return 0;
}
freeman [builder88]   2005-08-13 15:55 X
조금 초보자도 접근 가능하게 부스트에 대해서 자세하게
설명해 주실수 없나요, 왜 부스트를 쓰야 하는지 그리고
코드에 주석을 .... 

부탁 드릴께요
freeman [builder88]   2005-08-13 22:47 X
아래 코드가 컴파일이 안됩니다.
터보C 쪽에 있는 패치를 해도 에러가 나고,  부스트가 에럽네요......
초보자는 접근 어려버요....... 쩝.


//---------------------------------------


#include <iostream>
#include <locale>

#include <boost/regex.hpp>

using std::cout;
using std::wcout;
using std::endl;

void bad_search() {
  cout << "\nRegex++ can NOT support multibyte regex" << endl;
  std::string source = "アルファベットを含むShift_JIS文字列";
  boost::reg_expression<char> regex = "[a-zA-Z]. *[a-zA-Z]";
  boost::match_results<std::string::const_iterator> results;
  if ( boost::regex_search(source, results, regex) ) {
    cout << results. str(0) << endl;
  }
}

void good_search() {
  cout << "\nso let's try std::wstring & Regex++" << endl;
  std::wstring source = L"アルファベットを含むShift_JIS文字列";
  boost::reg_expression<wchar_t> regex = L"[a-zA-Z]. *[a-zA-Z]";
  boost::match_results<std::wstring::const_iterator> results;
  if ( boost::regex_search(source, results, regex) ) {
    wcout << results. str(0) << endl;
  }
}

void test_search() {

  cout << "\nregex_search for const char*" << endl;
  {
    const char* source = "here";
    boost::reg_expression<char> regex  = "(([a-z]+):)? //([^:/]+)(:([0-9]+))? /([a-zA-Z. 0-9]*)";
    boost::match_results<const char*> results;
    cout << "URL = " << source << endl
         << "regex = " << regex. str() << endl;
    if ( boost::regex_search(source, results, regex) ) {
      cout << " scheme :" << results. str(2) << endl
           << " host   :" << results. str(3) << endl
           << " port   :" << results. str(5) << endl
           << " path   :" << results. str(6) << endl;
    } else {
      cout << "no match. " << endl;
    }
  }

  cout << "\nregex_search for std::string" << endl;
  {
    std::string source = "here";
    boost::reg_expression<std::string::value_type> regex  = "(([a-z]+):)? //([^:/]+)(:([0-9]+))? /([a-zA-Z. 0-9]*)";
    boost::match_results<std::string::const_iterator> results;
    cout << "URL = " << source << endl
         << "regex = " << regex. str() << endl;
    if ( boost::regex_search(source, results, regex) ) {
      cout << " scheme :" << results. str(2) << endl
           << " host   :" << results. str(3) << endl
           << " port   :" << results. str(5) << endl
           << " path   :" << results. str(6) << endl;
    } else {
      cout << "no match. " << endl;
    }
  }

}

void test_wide_search() {

  cout << "\nregex_search for const wchar_t*" << endl;
  {
    const wchar_t* source = L"here";
    boost::reg_expression<wchar_t>  regex  = L"(([a-z]+):)? //([^:/]+)(:([0-9]+))? /([a-zA-Z. 0-9]*)";
    boost::match_results<const wchar_t*> results;
    wcout << L"URL = " << source << endl
          << L"regex = " << regex. str() << endl;
    if ( boost::regex_search(source, results, regex) ) {
      wcout << L" scheme :" << results. str(2) << endl
            << L" host   :" << results. str(3) << endl
            << L" port   :" << results. str(5) << endl
            << L" path   :" << results. str(6) << endl;
    } else {
      cout << "no match. " << endl;
    }
  }

  cout << "\nregex_search for std::wstring" << endl;
  {
    std::wstring source = L"here";
    boost::reg_expression<std::wstring::value_type> regex  = L"(([a-z]+):)? //([^:/]+)(:([0-9]+))? /([a-zA-Z. 0-9]*)";
    boost::match_results<std::wstring::const_iterator> results;
    wcout << L"URL = " << source << endl
          << L"regex = " << regex. str() << endl;
    if ( boost::regex_search(source, results, regex) ) {
      wcout << L" scheme :" << results. str(2) << endl
            << L" host   :" << results. str(3) << endl
            << L" port   :" << results. str(5) << endl
            << L" path   :" << results. str(6) << endl;
    } else {
      cout << "no match. " << endl;
    }
  }
}

struct grep_predicate {
  bool operator()(const boost::match_results<std::string::const_iterator>& m) {
    cout << m. str(0) << endl;
    return true;
  }
};

void test_grep() {
  cout << "\nregex_grep for std::string\n";
  const std::string             source   = "My name is same as a name of a month";
  boost::reg_expression<char> regex    = ". ame";
  boost::regex_grep(grep_predicate(), source. begin(), source. end(), regex);
}

struct grep_wide_predicate {
  bool operator()(const boost::match_results<std::wstring::const_iterator>& m) {
    wcout << m. str(0) << endl;
    return true;
  }
};

void test_wide_grep() {
  cout << "\nregex_grep for std::wstring\n";
  const std::wstring             source   = L"僕は海が好きで山が好きで川も好き";
  boost::reg_expression<wchar_t> regex    = L"(山|川)(.)好き";
  boost::regex_grep(grep_wide_predicate(), source. begin(), source. end(), regex);
}

int main() {
  std::locale::global(std::locale("japanese"));

  bad_search();
  good_search();

  test_search();
  test_wide_search();
  test_grep();
  test_wide_grep();

  return 0;
}
nicekr.황경록 [mpbox]   2005-08-14 19:07 X
음 ^^::: 저도 부스트쪽을 처음 보는거라 ^^''' 혹시 잘못된 정보를 전달할까봐서요 ^^::
STL 이나 그것을 왜 배워야 하는지는... 음... 그냥 놀구 있으면 뭐합니까 ㅋㅋ 그냥 해보는거죠. 뭐 이런 사고로 시작한거라 ^^ 좀더 머리속에 정리가 잘되면 도배성 글을 취합해서 정리해서 강좌란으로 옮기도록 하겠습니다. ^^
freeman [builder88]   2005-08-15 18:35 X
바쁘실 텐데 감사합니다..
조은 강좌 기대 하겠습니다.

+ -

관련 글 리스트
492 vector 와 any 와 shared_ptr 의 만남 첫번째 nicekr.황경록 8671 2005/08/12
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.