告诉cin停止阅读在换行 [英] tell cin to stop reading at newline
本文介绍了告诉cin停止阅读在换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我想从这样的输入中读取整数的行a:
Suppose I want to read line a of integers from input like this:
1 2 3 4 5\n
我想要cin停在'\\\
'字符,但cin似乎不认识它。
I want cin to stop at '\n' character but cin doesn't seem to recognize it.
下面是我使用的。
vector<int> getclause() {
char c;
vector<int> cl;
while ( cin >> c && c!='\n') {
cl.push_back(c);
cin>>c;
}
return cl;
}
我应该如何修改, n'character?
How should I modify this so that cin stop when it see the '\n' character?
推荐答案
使用getline和istringstream:
Use getline and istringstream:
#include <sstream>
/*....*/
vector<int> getclause() {
char c;
vector<int> cl;
std::string line;
std::getline(cin, line);
std::istringstream iss(line);
while ( iss >> c) {
cl.push_back(c);
}
return cl;
}
这篇关于告诉cin停止阅读在换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文