看流星社区

 找回密码
 注册账号
查看: 1941|回复: 0

C++ char* 空格分隔字符串

[复制链接]

该用户从未签到

发表于 2016-9-21 15:39:29 | 显示全部楼层 |阅读模式

越是简单的东西越容易出错。

vector test;
//char* p="  test data    ha   32ha.";
char *p = "12 23 56 78 99";
char* memory=p;
while (memory!=NULL)
{
while(*memory == ' ')
memory += 1;
  char a[20]={0};
  char* temp=strstr(memory," ");

if(temp == NULL && memory != NULL){
memcpy(a,memory,strlen(memory));
  test.push_back(a);
}
if (temp==NULL)
    break;

  memcpy(a,memory,temp-memory);
  test.push_back(a);
  while(*temp==' ')
    temp++;
  memory=temp;
}
cout<<test.size()<<endl;
vector::iterator Iter;
for (Iter=test.begin(); Iter!=test.end(); Iter++)
{
  cout<<*Iter<<endl;
}

整个程序是在百度空间里看到的。但是那个程序存在问题,对于字符串以空格开头的字符串就无法处理了。并且,在处理完字符串之后并没有将最后一个字符串加入到vector中。
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-3-19 22:04

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表