c++实现split函数

c++实现split函数

#include <vector>
#include <string>
#include <string.h>

bool SplitStream(const char* strSource, std::vector<char*>* strDest, const char seps)
{
    
    
	if (strSource == NULL || strDest == NULL)
	{
    
    
		return false;
	}
	const int MAX_CHAR = 500;//单个子串的长度,这里容易越界
	const char* pcSrc_Copy = strSource;
	char* pcOneData = new char[MAX_CHAR];
	memset(pcOneData, 0, MAX_CHAR);
	char p2[2] = {
    
     0 };
	memset(p2, 0, 2);

	int n = 0;
	//for (int i = 0; i < nStreamLen;i++)
	while (*pcSrc_Copy != '\0')
	{
    
    
		char Char_Current = *pcSrc_Copy++;
		if (Char_Current == seps)
		{
    
    
			strDest->push_back(pcOneData);
			pcOneData = new char[MAX_CHAR];
			memset(pcOneData, 0, MAX_CHAR);
			n = 0;
		}
		else
		{
    
    
			memcpy(pcOneData + n, &Char_Current, 1);
			n++;
		}
	}
	strDest->push_back(pcOneData);
	return true;
}

猜你喜欢

转载自blog.csdn.net/Richelieu_/article/details/83548796