C++将string转化成字符串数组

//str为需要截断的string,pattern为分隔符
std::vector<std::string> split(std::string str,std::string pattern) 
{  
	std::string::size_type pos;   
	std::vector<std::string> result;  
	str+=pattern;//扩展字符串以方便操作   
	int size=str.size();     
	for(int i=0; i<size; i++)   
	{     
		pos=str.find(pattern,i);     
		if(pos<size)     
		{      
			std::string s=str.substr(i,pos-i);       
			result.push_back(s);      
			i=pos+pattern.size()-1;     
		}   
	}   
	return result; 
}

  

posted @ 2015-07-17 10:25  striver_zhu  阅读(11691)  评论(0编辑  收藏  举报