string 拆分字符串
来源:互联网 发布:制作二维码的软件 编辑:程序博客网 时间:2024/04/29 07:56
以下C++实现一个将字符串拆分以后,存放到一个string当中的代码,使用的时候传入三个参数即可,非常好用:
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
using
namespace
std;
#include <vector>
inline
string& lTrim(string &ss)
{
string::iterator p=find_if(ss.begin(),ss.end(),not1(ptr_fun(
isspace
)));
ss.erase(ss.begin(),p);
return
ss;
}
inline
string& rTrim(string &ss)
{
string::reverse_iterator p=find_if(ss.rbegin(),ss.rend(),not1(ptr_fun(
isspace
)));
ss.erase(p.base(),ss.end());
return
ss;
}
inline
string& trim(string &st)
{
lTrim(rTrim(st));
return
st;
}
//将一个以par分割的整个字符串,分割以后存到vec里面
void
splitStr(vector<string> &vec, string str, string par){
string::size_type pos=0,previousPos=0;
//查找到的位置和前一个位置
//处理并没有分割符的情况
if
(str.find(par,0)==string::npos){
if
(!trim(str).empty()) vec.push_back(trim(str));
return
;
}
string strtmp;
for
( pos=str.find(par,0); pos!=string::npos; previousPos=pos+par.size(), pos=str.find(par,pos+par.size()) ){
strtmp = str.substr(previousPos,pos-previousPos);
if
(!trim(strtmp).empty()) vec.push_back(strtmp);
}
strtmp = str.substr(previousPos,pos-previousPos);
if
(!trim(strtmp).empty()) vec.push_back(strtmp);
}
#endif
本函数可以用以下示例调用:
- string 拆分字符串
- string字符串拆分、string转换成int
- 操作string字符串,拆分和截取
- Android中字符串String的拆分-split
- 将string类型的字符串按照空格拆分成多个字符串
- string 字符串拆分 二维dp(斜率优化)
- 拆分字符串
- 字符串拆分
- 拆分字符串
- 拆分字符串
- 拆分字符串
- 拆分字符串
- 拆分字符串
- 字符串拆分
- 拆分字符串
- 字符串拆分
- 拆分字符串
- 字符串拆分
- 自定义一个Toast(技巧)
- unity3d 鼠标点击 控制人物行走
- ORACLE查询索引在哪张表
- [微软面试100题] 设计包含min函数的栈
- MySql百万级数据库优化方案
- string 拆分字符串
- SQLServer2008 sa登录错误18456
- oracle查询某张表被其它表作为外键
- 《“十二五”节能环保产业发展规划》近日正式发布
- new、nothrow new和placement new 原理分析
- LIBSVM简介
- PHP操作Mongdb(4)----插入
- 关于小波变换的分析与思考
- Nagios插件编写及调试方法(个人经验版)