设为首页 加入收藏

TOP

学习笔记----字符串分割
2014-11-23 22:30:37 来源: 作者: 【 】 浏览:1
Tags:学习 笔记 ---- 字符串 分割

1, 字符串的分割算法(标准库版)
void split(const string& s,char c,vector& v)

{

string::size_type i = 0;

string::size_type j = s.find(c);

while (j != string::npos)

{

v.push_back(s.substr(i,j-i));

i = ++j;

j = s.find(c,j);

if (j == string::npos)

v.push_back(s.substr(i,s.length()) );

}

}

template

void split(basic_string& s,T c,vector >& v)

{

typename basic_string::size_type i = 0;

typename basic_string::size_type j = s.find(c);

while (j != basic_string::npos)

{

v.push_back(s.substr(i,j-i));

i = ++j;

j = s.find(c,j);

if (j == basic_string::npos)

{

v.push_back(s.substr(i,s.length()) );

}

}

}

2,字符串的分割算法(boost库实现)

int main()

{

string _str = "I|am|a|bad|boy";

list myList;

split(myList,_str,is_any_of("|")); // 这里用到了boost组库的方法:split.

list::iterator p;

for (p = myList.begin(); p != myList.end(); ++p)

{

cout << *p << " ";

}

cout << endl;

/***********************************************/

return 0;

}

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇获取操作系统CPU及内存使用信息的.. 下一篇有三种走楼梯方式,走完100阶总共..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: