C++分割字符串函数

博客包含分割 v,v 类型和 k,v|k,v 类型字符串的代码块,聚焦于字符串分割的信息技术内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分割 v,v 类型字符串代码块

void SetSub(string src, vector<int>& value)
{
	size_t size = 0;
	for (size_t i = 0; i < src.length(); ++i)
	{
		if (src.at(i) == ',')
		{
			size++;
		}
	}
	value = vector<int>(size + 1, 0);

	int  index = 0;
	int  count = 0;
	while ((index = src.find(',')) != -1)
	{
		value[count] = atoi(src.substr(0, index).c_str());
		src.erase(0, index + 1);
		count++;
	}
	value[count] = atof(src.c_str());
}


分割 k,v|k,v 类型字符串代码块

void SetSub(string src, std::map<int,int>& value)
{
	size_t size = 0;
	for (size_t i = 0; i < src.length(); ++i)
	{
		if (src.at(i) == '|')
		{
			size++;
		}
	}
	vector<string> temp = vector<string>(size + 1);

	int  index = 0;
	int  count = 0;
	while ((index = src.find('|')) != -1)
	{
		temp[count] = src.substr(0, index);
		src.erase(0, index + 1);
		count++;
	}
	temp[count] = src;
	
	for (int i=0;i<=count;i++)
	{
		int k = 0;
		string temp_str = temp[i];
		while ((k = temp_str.find(',')) != -1)
		{
			int id = atoi(temp_str.substr(0, k).c_str());
			if (id!=0)
			{
				value[id] = i;
			}
			temp_str.erase(0, k + 1);
		}
		int id_end = atof(temp_str.c_str());
		if (id_end!=0)
		{
			value[id_end] = i;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值