C++语言字符串分割,利用stringstream实现一个split函数

C++标准库没有string的分割函数,只能用C语言split函数,感觉不太方便。我们用stringstream实现一个简单的split函数:

void split(const string &str, vector<string> &vec, const char &c)
{
	stringstream ss(str);
	string sub;
	while (getline(ss, sub, c))
	{
		vec.push_back(sub);
	}
}

此实现代码固然简单,但出现多个连续的分隔符时不太符合我们的要求。没办法,我们还是用最原始的方法做吧。

void split(const string &str, vector<string> &vec, const char &c)
{
	string::size_type pos1 = 0;
	string::size_type pos2 = str.find(c, pos1);

	while(pos2 != string::npos)
	{
		vec.push_back(str.substr(pos1, pos2-pos1));

		pos1 = pos2 + sizeof(c);
		pos2 = str.find(c, pos1);
	}
	if(pos1 != str.length())
	{
		vec.push_back(str.substr(pos1));
	}
}

测试代码

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

void split(const string &str, vector<string> &vec, const char &c)
{
	string::size_type pos1 = 0;
	string::size_type pos2 = str.find(c, pos1);

	while(pos2 != string::npos)
	{
		vec.push_back(str.substr(pos1, pos2-pos1));

		pos1 = pos2 + sizeof(c);
		pos2 = str.find(c, pos1);
	}
	if(pos1 != str.length())
	{
		vec.push_back(str.substr(pos1));
	}
}
/*
void split(const string &str, vector<string> &vec, const char &c)
{
	stringstream ss(str);
	string sub;
	while (getline(ss, sub, c))
	{
		vec.push_back(sub);
	}
}
*/
void print_vec(vector<string> &vec)
{
	for(auto p = vec.begin(); p != vec.end(); p++)
		cout << *p << endl;
}

int main(void)
{
	vector<string> vec;

	string str1 = "hello world";
	string str2 = " hello world";
	string str3 = "hello world ";
	string str4 = " hello world ";
	string str5 = "  hello  world  ";

	cout << "[" << str1 << "]" << endl;
	vec.clear();
	split(str1, vec, ' ');
	cout << "----------begin----------" << endl;
	print_vec(vec);
	cout << "-----------end-----------" << endl;

	cout << "[" << str2 << "]" << endl;
	vec.clear();
	split(str2, vec, ' ');
	cout << "----------begin----------" << endl;
	print_vec(vec);
	cout << "-----------end-----------" << endl;

	cout << "[" << str3 << "]" << endl;
	vec.clear();
	split(str3, vec, ' ');
	cout << "----------begin----------" << endl;
	print_vec(vec);
	cout << "-----------end-----------" << endl;

	cout << "[" << str4 << "]" << endl;
	vec.clear();
	split(str4, vec, ' ');
	cout << "----------begin----------" << endl;
	print_vec(vec);
	cout << "-----------end-----------" << endl;
	
	cout << "[" << str5 << "]" << endl;
	vec.clear();
	split(str5, vec, ' ');
	cout << "----------begin----------" << endl;
	print_vec(vec);
	cout << "-----------end-----------" << endl;

	return 0;
}

运行结果

[ycxie@fedora Workspace]$ g++ str_split.cpp -o str_split -Wall
[ycxie@fedora Workspace]$ ./str_split
[hello world]
----------begin----------
hello
world
-----------end-----------
[ hello world]
----------begin----------

hello
world
-----------end-----------
[hello world ]
----------begin----------
hello
world
-----------end-----------
[ hello world ]
----------begin----------

hello
world
-----------end-----------
[  hello  world  ]
----------begin----------


hello

world

-----------end-----------

Leave a Reply

Your email address will not be published. Required fields are marked *