从字符串中解析数字的小程序

void AnalyzeStr2Number(vector<int>& v, const string& str, const std::string& separator)
{
	v.clear();
	if (str.empty())
		return;

	std::string strTmp;
	int pos1 = 0;
	int pos2 = 0;
	for (; (pos2 = str.find(separator, pos1)) != std::string::npos;)
	{
		strTmp = str.substr(pos1, pos2 - pos1);
		v.push_back(atoi(strTmp.c_str()));
		pos1 = pos2 + 1;
	}
}


int main()
{
	std::string str("11;22;33;44;55;");
	std::vector<int> v;
	AnalyzeStr2Number(v, str, ";");

	for (int i=0; i<v.size(); ++i)
		cout<<v[i]<<endl;
	return 0;
}

  

posted @ 2011-11-21 15:40  小 楼 一 夜 听 春 雨  阅读(577)  评论(0编辑  收藏  举报