以指定分隔符分割字符串存储到vector中

        实现用指定的分隔符把一个字符串分解成多个字符串,并把结果保存到一个vector中。

示例:
输入:
8:00起售车站:北京西、南京、南京南、同江

输出:
北京西
南京
南京南
同江

#include <iostream>
#include <vector>
#include <string>
using namespace std;

/********
vecStr: 用于存储结果的vector
strSource: 被分解的字符串
strSplit: 分隔符
nSkip:  开头忽略的字符数目,默认为0 
********/
void SplitStringToVector( vector<string> &vecStr, string strSource, string strSplit, int nSkip = 0 )
{
	vector<string>::size_type sPos = nSkip;
	vector<string>::size_type ePos = strSource.find( strSplit, sPos );
	while( ePos != string::npos )
	{
		if( sPos != ePos ) vecStr.push_back( strSource.substr( sPos, ePos - sPos ) );
		sPos = ePos + strSplit.size();    
		ePos = strSource.find( strSplit, sPos );
	}    
	if( sPos < strSource.size() ) vecStr.push_back( strSource.substr( sPos, strSource.size() - sPos ) ); 
}

int main()
{
    string str1 = "8:00起售车站:北京西、南京、南京南、同江";
    vector<string> vecStr;
    SplitStringToVector( vecStr, str1, "、", 13 );
    for( vector<string>::iterator iter = vecStr.begin(); iter != vecStr.end(); iter++ )
        cout<<*iter<<endl;
    return 0;   
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值