c++分割字符串
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
int main(void)
{
string str = "1234 5678 90 99";
const char* s = str.c_str();
int i = 0;
while(*s)
{
if(*s == ' ')
{
string a = str.substr(0,i);
cout << a.c_str() << endl;
str = s + 1;
i = 1;
}
++s;
++i;
}
}
这按空格分割字符串为什么str=s+1,执行完成后s指向的内容就变了。