« C++でStringTokenizerっぽいことをやってみる | Home | MacBookPro 購入 »

June 14, 2009

続・C++でStringTokenizerっぽいことをやってみる

 前回の記事haveの指摘に従い、参照渡しをやめてヴェクタを返す関数にしてみた。

std::vector<string> 
slice(const std::string source, const std::string dem)
{
  std::string::size_type i1 = 0;
  std::string::size_type i2 = source.find(dem);
  std::vector<string> retval;

  if(std::string::npos == i2)
    return retval;

  std::string s = source + dem;

  do{
    retval.push_back(s.substr(i1, i2-i1));
    i1 = i2+1;
  }while(std::string::npos != (i2 = s.find(dem, i1)));

  return retval;
}

 ちょっと効率は落ちたが、Atomicになったので前回のコードにくらべ、マルチスレッドプログラミングで安全に使えるようになった。

Thanks! nice comment > Have

No TrackBacks

TrackBack URL: http://www.argv.org/~chome/blog/mt-tb.cgi/184

About this Entry

This page contains a single entry by chomy published on June 14, 2009 3:16 AM.

C++でStringTokenizerっぽいことをやってみる was the previous entry in this blog.

MacBookPro 購入 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.