C++实现从输入中读取字符串
你可以用这种方式读取一个单独的以空格结束的词:
#include<iostream>
#include<string>
usingnamespacestd;
intmain(){
cout<<"Pleaseenteraword:\n";
strings;
cin>>s;
cout<<"Youentered"<<s<<'\n';
}
注意,这里没有显式的内存管理,也没有可能导致溢出的固定大小的缓冲区。
如果你确实想得到一行而不是一个单独的词,可以这样做:
#include<iostream>
#include<string>
usingnamespacestd;
intmain(){
cout<<"Pleaseenteraline:\n";
strings;
getline(cin,s);
cout<<"Youentered"<<s<<'\n';
}热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志