C++ STL 標準ライブラリ split 文字列分割
#include <string>
#include <list>
#include <iostream>
using namespace std;
/**
* split関数
* @param string str 分割したい文字列
* @param string delim デリミタ
* @return list<string> 分割された文字列
*/
list<string> split(string str, string delim)
{
list<string> result;
int cutAt;
while( (cutAt = str.find_first_of(delim)) != str.npos )
{
if(cutAt > 0)
{
result.push_back(str.substr(0, cutAt));
}
str = str.substr(cutAt + 1);
}
if(str.length() > 0)
{
result.push_back(str);
}
return result;
}
/*
* split関数をテストしてみる。
*/
int main()
{
// カンマ区切りの文字列を用意します。
string str = "aaa,bbb,ccc";
// splitを実行します。
list<string> strList = split(str, ",");
// イテレータを取得します。
list<string>::iterator iter = strList.begin();
// 侮ヲしてみます。
while( iter != strList.end() ) // 最後まで
{
std::cout << *iter << std::endl;
// 1つ後方に進む
++iter;
}
return 0;
}
aaa
bbb
ccc
http://goodjob.boy.jp/chirashinoura/id/100.html
作成日: 2006-09-12 19:32:45
最終更新日: 2007-04-03 23:02:13
▲このページの上へ管理人: ぶらざーほわいつ 連絡