看代码:

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

int Str2Vector(string str, vector<string> &v, const char *p)
{
	v.clear();
	char *pTmp =  strtok(const_cast<char *>(str.c_str()), p);
	while(NULL != pTmp)
	{
		v.push_back(pTmp);
		pTmp = strtok(NULL, p);
	}
	
	return v.size();
}

int main()
{
	string s = "|ab|cd|efg|h|";
	string t = s;
	vector<string> v;
	Str2Vector(s, v, "|");
	
	for(vector<string>::iterator it = v.begin(); it != v.end(); ++it)
	{
		cout << *it << endl;
	}
	
	cout << s << endl;
	cout << t << endl;
	
	return 0;
}
      结果:

ab
cd
efg
h
|abcdefgh
|abcdefgh


       没有采取引用方式, 但s和t的值都变了。 因为strtok恶心。 不多说。







本文转载:CSDN博客