首页 > 编程 > C++ > 正文

C++实现从输入中读取字符串

2020-05-23 14:03:58
字体:
来源:转载
供稿:网友
这篇文章主要介绍了C++实现从输入中读取字符串的实现思路和具体代码,非常的简单实用,有需要的小伙伴可以参考下
 

你可以用这种方式读取一个单独的以空格结束的词:

#include<iostream>#include<string>using namespace std;int main(){  cout << "Please enter a word:/n";  string s;  cin>>s;  cout << "You entered " << s << '/n';}

注意,这里没有显式的内存管理,也没有可能导致溢出的固定大小的缓冲区。

如果你确实想得到一行而不是一个单独的词,可以这样做:

#include<iostream>#include<string>using namespace std;int main(){  cout << "Please enter a line:/n";  string s;  getline(cin,s);  cout << "You entered " << s << '/n';}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表