最近我遇到一个问题
但在此之前我会告诉你参考是什么
考虑这个程序
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string> RS;
string word;
while(cin>>word)
RS.push_back(word);
}
此代码将间隔字符串的每个单词存储在向量中
但问题就来了......
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<string> RS,FS;
string word;
while(cin>>word)
RS.push_back(word);
while(cin>>word)
FS.push_back(word);
}
这里的动机是将第一行的字符串单词存储在RS中
和 FS 向量中的第二行
但它不会停在一行末尾并将所有单词存储在 RS 中
并且 FS 保持为空。
请建议一种正确执行相同程序的方法
或者
如果您知道更有效的方法,我们非常欢迎
提前致谢
Use getline
and istringstream
,分别为每个句子,然后将其中的每个单词推回:
string line;
getline(cin, line); //Get sentence 1
istringstream iss1(line);
while ( iss1 >> word) {
RS.push_back(word);
}
getline(cin, line); //Get sentence 2
istringstream iss2(line);
while ( iss2 >> word) {
FS.push_back(word);
}
换行符 ('\n') 充当定界字符 for getline()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)