【发布时间】:2011-06-22 22:44:33
【问题描述】:
我想让 cin 从字符串中读取输入。
有没有办法让它这样做?
类似这样的:
const char * s = "123 ab";
cin.readFrom(s);//<---- I want something like this
int i;
cin>>i;
cout<<i; //123
【问题讨论】:
-
如果你想使用 char 数组而不是 std:string 你可以使用 cin.getline (s,n);其中 n 是“要存储的字符数(包括终止空字符)”。见cplusplus.com/reference/iostream/istream/getline。 std::string 也有一个 getline,见 cplusplus.com/reference/string/getline
-
@anno:从字符串中读取
cin,而不是从cin中读取字符串。