![](https://seed.istak.org.tw/attachment.php?aid=MTU1OTB8MzYyYjgwMWJ8MTY5NDc3NTk5N3xjNDlhK3NyM0xlRk05NWRtTzJwNWNrSG9MUnNnS3BkUWs1cFIvdzk0R1ZyTzZVOA%3D%3D&noupdate=yes)
getline(cin,str)
接收一個字串,可以接收空格並輸出
ex:可以讀單字、句子- #include<iostream>
- #include<cstdlib>
- #include<string>
- using namespace std;
- int main()
- {
- string str1;
- cout<<"請輸入一字串(包含空白): ";
- getline(cin,str1);
- cout<<"您剛輸入的字串是: "<<str1<<endl;
- string str2;
- cout<<"請輸入一字串(不包含空白): ";
- cin>>str2;
- cout<<"您剛輸入的字串是: "<<str2<<endl;
-
- system("pause");
- return 0;
- }
複製代碼 |