gpt4 book ai didi

结构的c++ vector 和从文件中读取

转载 作者:行者123 更新时间:2023-11-30 05:04:57 25 4
gpt4 key购买 nike

所以,我有一个正在读取的文件,其中包括球员姓名,然后是空格,后跟球队名称 [Christian_Ponder KC]。为此,我制作了这个结构:

struct qb_info {
string player;
string team;
};

然后,在 main 中,我读取一个字符串 vector 中的所有内容:字符串 str;

vector <string> players_and_team;
qb_info player_info;
vector <qb_info> player_data;

while(!data_file.eof()) {
data_file >> str;
players_and_team.push_back(str);
}

然后,如果索引是偶数,它将是一个玩家,所以我调用结构并将其存储在玩家中并将它插入结构的 vector 中,如果它是奇数,它将是团队:

for(int i = 0; i < players_and_team.size(); i++) {
if(i % 2 == 0) {
player_info.player = players_and_team.at(i);
player_data.push_back(player_info);
} else {
player_info.team = players_and_team.at(i);
player_data.push_back(player_info);
}
}

但是当我打印它时,所有内容都打印了两次:

for(int i = 0; i < player_data.size() / 2; i++) {
cout << player_data.at(i).player << ", " << player_data.at(i).team << endl;
}

这是输出:

Aaron_Rodgers,  
Aaron_Rodgers, GB
Alex_Smith, GB
Alex_Smith, KC
Andrew_Luck, KC
Andrew_Luck, IND
Andy_Dalton, IND
Andy_Dalton, CIN
Austin_Davis, CIN
Austin_Davis, STL
Ben_Roethlisberger, STL
Ben_Roethlisberger, PIT
Blaine_Gabbert, PIT
Blaine_Gabbert, SF
Blake_Bortles, SF
Blake_Bortles, JAC
Brandon_Weeden, JAC
Brandon_Weeden, DAL
Brian_Hoyer, DAL
Brian_Hoyer, CLE
Brock_Osweiler, CLE
Brock_Osweiler, DEN
Cam_Newton, DEN
Cam_Newton, CAR
Carson_Palmer, CAR
Carson_Palmer, ARI
Case_Keenum, ARI
Case_Keenum, HOU
Chad_Henne, HOU
Chad_Henne, JAC
Charlie_Whitehurst, JAC
Charlie_Whitehurst, TEN
Chase_Daniel, TEN
Chase_Daniel, KC
Christian_Ponder, KC
Christian_Ponder, MIN
Colin_Kaepernick, MIN
Colin_Kaepernick, SF
Colt_McCoy, SF
Colt_McCoy, WAS
Connor_Shaw, WAS
Connor_Shaw, CLE
Derek_Anderson, CLE
Derek_Anderson, CAR
Derek_Carr, CAR
Derek_Carr, OAK
Drew_Brees, OAK
Drew_Brees, NO
Drew_Stanton, NO
Drew_Stanton, ARI
EJ_Manuel, ARI
EJ_Manuel, BUF
Eli_Manning, BUF
Eli_Manning, NYG
Geno_Smith, NYG
Geno_Smith, NYJ
Aaron_Rodgers, NYJ
Aaron_Rodgers, GB
AJ_McCarron, GB
AJ_McCarron, CIN
Alex_Smith, CIN
Alex_Smith, KC
Alex_Tanney, KC
Alex_Tanney, TEN
Andrew_Luck, TEN
Andrew_Luck, IND
Andy_Dalton, IND
Andy_Dalton, CIN
Austin_Davis, CIN
Austin_Davis, CLE
B.J._Daniels, CLE
B.J._Daniels, HOU
Ben_Roethlisberger, HOU
Ben_Roethlisberger, PIT
Blaine_Gabbert, PIT
Blaine_Gabbert, SF
Blake_Bortles, SF
Blake_Bortles, JAC
Brandon_Weeden, JAC
Brandon_Weeden, HOU
Brian_Hoyer, HOU
Brian_Hoyer, HOU
Brock_Osweiler, HOU
Brock_Osweiler, DEN
Cam_Newton, DEN
Cam_Newton, CAR
Carson_Palmer, CAR
Carson_Palmer, ARI
Case_Keenum, ARI
Case_Keenum, STL
Charlie_Whitehurst, STL
Charlie_Whitehurst, IND
Chase_Daniel, IND
Chase_Daniel, KC
Colin_Kaepernick, KC
Colin_Kaepernick, SF
Colt_McCoy, SF
Colt_McCoy, WAS
Dan_Orlovsky, WAS
Dan_Orlovsky, DET
Derek_Anderson, DET
Derek_Anderson, CAR
Derek_Carr, CAR
Derek_Carr, OAK
Drew_Brees, OAK
Drew_Brees, NO
Drew_Stanton, NO
Drew_Stanton, ARI
EJ_Manuel, ARI
EJ_Manuel, BUF
Eli_Manning, BUF
Eli_Manning, NYG
Geno_Smith, NYG
Geno_Smith, NYJ

有人能指出我做错事的正确方向吗?

最佳答案

这里发生的是你重用已经填充的qb_info player_info

首先你插入“Aaron_Rodgers”并打印它

Aaron_Rodgers,

接下来将“GB”添加到 player_info

Aaron_Rodgers, GB

接下来保留“GB”并插入“Alex_Smith”

Alex_Smith, GB

接下来您保留“Alex_Smith”并添加“KC”

Alex_Smith, KC

接下来您保留“KC”并插入“...”,依此类推。


您可以采纳@Someprogrammerdude 的建议并同时读取这两个值,这将简化您的程序

std::vector<qb_info> player_data;
qb_info player_info;
while (data_file >> player_info.player >> player_info.team) {
player_data.emplace_back(player_info);
}

然后可以在 vector 上的一个简单循环中完成打印

for (auto &i : player_data) {
std::cout << i.player << ", " << i.team << '\n';
}

关于结构的c++ vector 和从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48618751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com