自己编写的字符串分割函数mysplit()

利用strtok()函数封装成的字符串分割函数mysplit()
sourceStr为源字符串
splitSymbol为分隔符号
strArray为分割后的字符串,以vector形式返回

#include
#include
#include
#include
using namespace std;
void mysplict(string sourceStr, char *splitSymbol, vector &strArray)
{
	string temp;
	char *str = new char[sourceStr.size()];
	sourceStr.copy(str, sourceStr.size(), 0); 
	*(str + sourceStr.size()) = '\0'; //要手动加上结束符
	char *p2 = strtok(str, splitSymbol);
	while (p2 != NULL)
	{
		//cout << p2 << endl;
		temp = p2;
		cout << temp << endl;
		strArray.push_back(temp);
		p2 = strtok(NULL, splitSymbol);
	}

}
int main()
{
	string temp;
	vector number;
	while (getline(cin, temp))		//while (cin>> temp)
	{
		cout << temp<

输入:1, 2 2,3 45 , 6 7
输出:1, 2 2,3 45 , 6 7
1
2
2
3
45
6
7

你可能感兴趣的:(C++)