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

时间:2018-02-05 08:54:51

标签: c++ vector struct ifstream

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

struct qb_info {
    string player;
    string team;
};

然后,在main中,我正在读取一个字符串向量中的所有内容: string 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);
}

然后,如果索引是偶数,它将成为一个玩家,所以我调用结构并存储在播放器中并将其推送到struct的向量中,如果它奇怪,它将是团队:

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

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

2 个答案:

答案 0 :(得分:1)

这里发生的是您重复使用已填充的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);
}

然后可以在矢量

上的简单循环中完成打印
for (auto &i : player_data) {
    std::cout << i.player << ", " << i.team << '\n';
}

答案 1 :(得分:0)

您希望将具有两个成员的数据结构推送到向量中,您不能像这样单独分配玩家名称及其团队,只有当您同时拥有这两个值时,才应将数据结构推回到向量中:

>>> list(pd.get_dummies(test).values)
[array([1, 0, 0], dtype=uint8), array([1, 0, 0], dtype=uint8), array([0, 1, 0], dtype=uint8), array([0, 1, 0], dtype=uint8), array([0, 0, 1], dtype=uint8), array([0, 1, 0], dtype=uint8)]
>>>

在此代码中,在player_info同时拥有玩家和团队数据后,它会被推送到向量一次。