输入整行字符串 用cin的>>操作符输入字符串,会以空格作为分隔符,空格后的内容会在下一回输入时被读取
用string头文件中的getline可以输入整行字符串,例如: getline(cin, s2);
以其它字符作为分隔符输入字符串
输入字符串时,可以使用其它分隔符作为字符串结束的标志(例如逗号、分号)
把分隔符作为getline的第3个参数即可,例如: getline(cin, s2, ',');
例:
include <iostream>
#include <string>
using namespace std;
int main() {
for (int i = 0; i < 2; i++) {
string city, state;
getline(cin, city, ',');
getline(cin, state);
cout << "City:" << city << “ State:" << state << endl;
}
return 0;
}