Const_iterator 成员变量在初始化后未指向 vector 成员变量的开头

Const_iterator member variable not pointing to the begin of a vector member variable after initialization

我正在尝试为纸牌游戏 Avalon 实施贝叶斯估计器。游戏有五轮,每轮最多包含五个不同玩家提出的五个建议。如果提议被接受,玩家将继续进行任务,游戏将进入下一轮。在上一轮比赛结束之前,还不知道下一轮将有哪5位选手获得推荐队伍。我想跟踪当前使用迭代器提议团队的玩家,但不知何故它最终指向无处。具体来说,在调用 round1 的构造函数中,迭代器 Round::proposer 正确指向 &PlayerA,即 Round::proposers 的开头。但是,当我将此实例(或其副本?)添加到 Game::rounds 时,Game::rounds.back() 的 Round 成员 proposer 指向任何地方,即使 Round 成员 proposers仍然是正确的。为什么会这样?在执行过程中,当然会在调用(*Round::proposer)->make_proposal();期间抛出读取访问冲突异常。对于冗长的问题,我深表歉意,但似乎需要两级间接才能产生错误。

// Player.h
#include <string>

class Player
{
private:
    std::string name;
public:
    Player(std::string name) : name(name) {};
    void make_proposal() const {};
};
// Round.h
#include "Player.h"
#include <vector>

class Round
{
private:
    std::vector<const Player*> proposers;
    std::vector<const Player*>::const_iterator proposer;
public:
    Round(std::vector<const Player*> proposers) : proposers(proposers), proposer(Round::proposers.begin()) {};

    void next_proposal() { (*Round::proposer)->make_proposal(); };
};
// Game.h
#include "Round.h"
#include <vector>

class Game
{
private:
    std::vector<Player*> players;
    std::vector<Player*>::iterator active_player;
    std::vector<Round> rounds;
public:
    Game(std::vector<Player*> players);

    void advance_player();
    void next_round();
};
// Game.cpp
#include "Game.h"

Game::Game(std::vector<Player*> players)
    : players(players), active_player(Game::players.begin())
{
    std::vector<Player*>::const_iterator player = Game::players.begin();
    std::vector<const Player*> proposers = { *player };
    for (unsigned int i = 0; i < 4; ++i) {
        ++player;
        if (player == Game::players.end()) player = Game::players.begin();
        proposers.push_back(*player);
    }

    Round round1(proposers);
    Game::rounds = { round1 };
}

void Game::next_round()
{
    Game::rounds.back().next_proposal();
}
#include <iostream>
#include "Game.h"

int main()
{
    Player playerA("A");
    Player playerB("B");
    Player playerC("C");
    Player playerD("D");
    Player playerE("E");
    Player playerF("F");

    std::vector<Player*> players = { &playerA, &playerB, &playerC, &playerD, &playerE, &playerF };
    Game game(players);

    for(unsigned int i = 0; i < 5; ++i) {
        game.next_round();
    }
}

令人惊讶的是,替换了这两行代码

Round round1(proposers);
Game::rounds = { round1 };

Game.cpp

Round* round1 = new Round(proposers);
Game::rounds = { *round1 };

解决了这个问题,虽然我真的不明白为什么。毕竟roundsGame的成员变量,一直存在到实例game被销毁。这个 hack 的后续问题:round1 指向的最后一个代码片段中的实例是否被 class Game 的默认构造函数销毁,因为它在添加到成员变量之前被取消引用?

您的Round无法正常复制:

class Round
{
private:
    std::vector<const Player*> proposers;
    std::vector<const Player*>::const_iterator proposer;
public:
    Round(std::vector<const Player*> proposers) : proposers(proposers), proposer(Round::proposers.begin()) {};

    void next_proposal() { (*Round::proposer)->make_proposal(); };
};

如果您复制它,proposer 仍将是原始 Round 中元素的迭代器,而不是副本中向量的迭代器。当您这样做时:

Round* round1 = new Round(proposers);
Game::rounds = { *round1 };

然后局部对象 round1 在作用域的末尾销毁,因此迭代器现在在 rounds 内,在创建一个round1 的副本引用了一个仍然存在的元素。尽管它指的是 round1 中的元素,而不是您放置在 rounds.

中的 Round

要么注意 3/5Round 的规则,要么使用索引而不是迭代器。复制整个向量时,索引不会失效。 (当您将更多元素推回向量时,它们也不会失效,但迭代器会)


类似问题的更简单示例:

#include <iostream>
struct broken {
    int x;
    int* ptr;
    broken(int a = 0) : x(a),ptr(&x) {}
};

int main() {
    broken a{42};
    broken b{123};
    a = b;
    a.x = 0;
    std::cout << *(a.ptr);
}

b 复制到 a 后,a 中的指针仍将指向 b.x,因此输出为 123(而不是 0 正如人们所料)。