多次改组矢量后读取访问冲突

时间:2018-01-09 17:10:04

标签: c++ vector shuffle

对于一个学校项目,我需要用一个独特的指针来移动一个矢量。 但是,当我这样做时,我会在一段时间后收到读取访问冲突错误。 我创建了向量,然后调用shuffle。我多次这样做。我只是用值创建向量并将其洗牌一次,几次后我得到读取访问冲突错误。

    int RandomEngine::generateRandomInt(int minValue, int maxValue)
    {
        std::uniform_int_distribution<int> distribution(minValue, maxValue);
        std::cout << "Random getal van " << minValue << " tot " << maxValue << std::endl;
        return distribution(def_rand_engine);
    }

    void RandomEngine::shuffleCharacterCards(std::vector<std::unique_ptr<CharacterCard>>& cards)
    {
        // Succeeds once but with multiple swaps read access violation
        //auto randomInt = generateRandomInt(0, cards.size() - 1);
        //iter_swap(cards.begin() + randomInt, cards.begin());

        // Read access violation
        //for (int i = 0; i < cards.size(); i++)
        //{
        //  std::swap(cards[i], cards[std::rand() % cards.size()]);
        //}

        // Read access violation
        //std::shuffle(cards.begin(), cards.end(), std::mt19937{ def_rand_engine});

        // Read access violation
        //std::random_shuffle(cards.begin(), cards.end());



    // Read access violation
    //std::shuffle(cards.begin(), cards.end(), std::mt19937(std::random_device()()));
}

我从静态类

调用shuffle的其他类
void CharacterCardStack::prepare()
{
    std::cout << "Preparing CharacterCardStack..." << std::endl;
    if(cards_.size() > 2)
    {
        // Shuffle the cards
        RandomEngine::shuffleCharacterCards(cards_);

        // Burn the first card
        burnedCharacterCard_ = std::move(cards_[0]);
        std::cout << "Burned charactercard: " << burnedCharacterCard_->getName() << std::endl;
        cards_.erase(cards_.begin());
        // Als de open kaart een koning is dan moet deze vervangen worden door een ander.
        if(cards_[0]->getName() == "Koning")
        {
            turnedCharacterCard_ = std::move(cards_[1]);
        }
        else
        {
            turnedCharacterCard_ = std::move(cards_[0]);
        }
        std::cout << "Turned charactercard: " << turnedCharacterCard_->getName() << std::endl;
        cards_.erase(cards_.begin());
    }
    else
    {
        std::cerr << "CharacterCardStack::prepare cards size is " << cards_.size() << std::endl;
        throw std::exception("Error...");
    }
    for (const auto& c : cards_)
    {
        std::cout << "Other card: " << c->getName() << std::endl;
    }
    std::cout << "CharacterCardStack prepared" << std::endl;
}

1 个答案:

答案 0 :(得分:0)

你的逻辑中有流动,你根据条件移出第一个或第二个元素:

    if(cards_[0]->getName() == "Koning")
    {
        turnedCharacterCard_ = std::move(cards_[1]);
    }
    else
    {
        turnedCharacterCard_ = std::move(cards_[0]);
    }

然后无条件地删除第一个元素:

cards_.erase(cards_.begin());

所以你最终可能会移出指针。

简单修复可能是:

    if(cards_[0]->getName() == "Koning")
    {
        std::swap( cards_[0], cards_[1] );
    }
    turnedCharacterCard_ = std::move(cards_[0]);
    std::cout << "Turned charactercard: " << turnedCharacterCard_->getName() << std::endl;
    cards_.erase(cards_.begin());

通过这种方式更容易保持正确,我更倾向于使用cards.front()代替cards_[0],在这种情况下我认为更具可读性。