尝试使用两个单独的 getline() 实例来填充两个单独的向量
Trying to use two separate instances of getline() to fill two separate vectors
我正在研究 this HackerRank problem。
在我的问题中,我试图使用 getline()
将两行不同的输入输入到两个单独的向量中以用于组织目的。
输入如下所示:
9 6 2015
6 6 2015
这是我的代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
#include <sstream>
using namespace std;
int main() {
vector<int> expecDate;
vector<int> retDate;
string firstLine;
string secLine;
while(getline(cin, firstLine)){
stringstream ss(firstLine);
while(getline(ss,firstLine,' ')){
int num = atoi(firstLine.c_str());
retDate.push_back(num);
}
}
while(getline(cin, secLine)){//want to use this second instance of getline() to fill the expecDate vector
stringstream ss_2(secLine);
while(getline(ss_2,secLine,' ')){
int num_2 = atoi(secLine.c_str());
expecDate.push_back(num_2);
}
}
int year_e, month_e, day_e;
int year_a, month_a, day_a;
year_a = retDate[2];
month_a = retDate[1];
day_a = retDate[0];
year_e = retDate[5];//want this to be expecDate[2]
month_e = retDate[4];//want this to be expecDate[1]
day_e = retDate[3];//want this to be expecDate[0]
if(year_a <= year_e && month_a <= month_e && day_a <= day_e){
cout<<"0"<<endl;
}else if(year_a > year_e){
cout<<"10000"<<endl;
}else if(month_a > month_e){
int total_m = 500*(month_a - month_e);
cout<<total_m<<endl;
}else if(day_a > day_e){
int total_d = 15*(day_a - day_e);
cout<<total_d<<endl;
}
return 0;
}
这段代码 returns 是正确的输出,我只是想知道如何制作它以便我可以使用两个向量而不是 getline()
只需完全填充第一个 vector
.
更新:使用此代码,expecDate 根本不会被填充。尝试引用其任何成员会导致分段错误。
您的代码中 while
的使用不正确。
while(getline(cin, firstLine)){
...
}
将继续从 cin
读取数据,直到没有可读取的数据为止。之后,
while(getline(cin, secLine)){
...
}
不执行任何操作,因为 cin
中没有任何内容可供读取。
不要使用 while
。使用 if
.
if (getline(cin, firstLine)){
...
}
if (getline(cin, secLine)){
...
}
我正在研究 this HackerRank problem。
在我的问题中,我试图使用 getline()
将两行不同的输入输入到两个单独的向量中以用于组织目的。
输入如下所示:
9 6 2015
6 6 2015
这是我的代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include<string>
#include <sstream>
using namespace std;
int main() {
vector<int> expecDate;
vector<int> retDate;
string firstLine;
string secLine;
while(getline(cin, firstLine)){
stringstream ss(firstLine);
while(getline(ss,firstLine,' ')){
int num = atoi(firstLine.c_str());
retDate.push_back(num);
}
}
while(getline(cin, secLine)){//want to use this second instance of getline() to fill the expecDate vector
stringstream ss_2(secLine);
while(getline(ss_2,secLine,' ')){
int num_2 = atoi(secLine.c_str());
expecDate.push_back(num_2);
}
}
int year_e, month_e, day_e;
int year_a, month_a, day_a;
year_a = retDate[2];
month_a = retDate[1];
day_a = retDate[0];
year_e = retDate[5];//want this to be expecDate[2]
month_e = retDate[4];//want this to be expecDate[1]
day_e = retDate[3];//want this to be expecDate[0]
if(year_a <= year_e && month_a <= month_e && day_a <= day_e){
cout<<"0"<<endl;
}else if(year_a > year_e){
cout<<"10000"<<endl;
}else if(month_a > month_e){
int total_m = 500*(month_a - month_e);
cout<<total_m<<endl;
}else if(day_a > day_e){
int total_d = 15*(day_a - day_e);
cout<<total_d<<endl;
}
return 0;
}
这段代码 returns 是正确的输出,我只是想知道如何制作它以便我可以使用两个向量而不是 getline()
只需完全填充第一个 vector
.
更新:使用此代码,expecDate 根本不会被填充。尝试引用其任何成员会导致分段错误。
您的代码中 while
的使用不正确。
while(getline(cin, firstLine)){
...
}
将继续从 cin
读取数据,直到没有可读取的数据为止。之后,
while(getline(cin, secLine)){
...
}
不执行任何操作,因为 cin
中没有任何内容可供读取。
不要使用 while
。使用 if
.
if (getline(cin, firstLine)){
...
}
if (getline(cin, secLine)){
...
}