切割字符串代码

str.assign("if this is a joke; i would like to know");

	string tempstr = str;

	if(!tempstr.empty())
	{
		string::iterator it = tempstr.begin();
		int word_len = 0;
		while(it!=tempstr.end())
		{			
			if(isalnum(*it))
			{
				++word_len;
				++it;
			}
			else
			{
				
				if(word_len!=0)
				{
					string temp;
					temp.assign(it-word_len,it);
					str_vector.push_back(temp);
					word_len = 0;
					++it;
				}
				else
					++it;
			}
		}
		if(word_len!=0)
		{
				string temp;
				temp.assign(it-word_len,it);
				str_vector.push_back(temp);
				word_len = 0;
		}

	}

	vector<string>::iterator vector_it =str_vector.begin();

	while(vector_it != str_vector.end())
	{
		cout << *vector_it << endl;;
		++vector_it;
	}
 

你可能感兴趣的:(切割字符串代码)