C++ 字符串数组有它自己的类,所以想独立出来处理。
如下面代码是string的遍历,已经根据空格来分离string的字符串。
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main(void)
{
string s;
string tmp;
unsigned int i;
cout << "Input strings and end with empty string:" <<endl;
getline(cin, tmp);
s = s + tmp;
cout << "The strings is:" << s << endl;
tmp = ""; // empty tmp string
for (i=0; i < s.size(); ++i){
if (' ' == s[i]){
cout << tmp << endl;
tmp = "";
} else {
tmp = tmp + s[i];
if (i == s.size() - 1)
cout << tmp << endl;
}
}
return 0;
}