string idol, boy;
cin>>idol;
getline(cin,boy);
cout << "idol is " << idol << endl
<< "boy is " << boy << endl;
若第一次输入的字符中包括空格,空格之后的内容会舍去或者输入到下一个变量中。
以上若输入“if you can try”,当按enter之后,程序即输出结果,输出结果为:
idol is if
boy is you can try
若调换输入的顺序:
string idol, boy;
getline(cin,boy);
cin>>idol;
cout << "idol is " << idol << endl
<< "boy is " << boy << endl;
以上输入“if you can try”并按回车键之后,程序会要求输入另一个字符,若为“you would find”,结果会是:
idol is you
boy is if you can try