字符串分离函数

http://www.cplusplus.com/reference/clibrary/cstring/strtok/   看一下strtok()函数就会懂了... 另外还有更好用的 istringstream
#include
#include
#include 
#include 

using namespace std ; 

void getValues(const char* source)
{
	int value1 ,value2 ;
	char* cvalue; 
	char tmpStr[256] ;
	
	strcpy(tmpStr,source);
	
	cvalue = strtok(tmpStr, ",;");
	value1 = atoi(cvalue);
	printf("value1: %d\n",value1);
	
	cvalue = strtok(NULL,",;");
	value2 = atoi(cvalue);
	printf("value2: %d\n",value2);
	
	while( cvalue!=NULL  )
	{
		///使用两个分离符进行分离时会出现先后次序的问题,导致多进行了一次判断 
		cvalue = strtok(NULL, ",;"); 
		value1 = atoi(cvalue);
		printf("value1: %d\n",value1);
	
		cvalue = strtok(NULL,",;");
		value2 = atoi(cvalue);
		printf("value2: %d\n",value2);
	}
}

int main()
{
	istringstream istr("-99,26;");
	string strTemp;//输出到strTemp中 
	char c ; 
	int value ; 
	int first ; 
	
	istr>>first ;
	cout<>c ;
	cout<>first ;
	cout<>c ;
	cout<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值