指定した区切り文字列で文字列を分割してvectorに格納

1196 ワード

インプリメンテーションは、指定した区切り記号で1つの文字列を複数の文字列に分解し、結果を1つのvectorに保存します.
例:
入力:
8:00から販売駅:北京西、南京、南京南、同江
出力:
北京西
南京
南京南
同江
#include 
#include 
#include 
using namespace std;

/********
vecStr:        vector
strSource:        
strSplit:    
nSkip:           ,   0 
********/
void SplitStringToVector( vector &vecStr, string strSource, string strSplit, int nSkip = 0 )
{
	vector::size_type sPos = nSkip;
	vector::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 vecStr;
    SplitStringToVector( vecStr, str1, "、", 13 );
    for( vector::iterator iter = vecStr.begin(); iter != vecStr.end(); iter++ )
        cout<