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;
- }
複製代碼 |