0

解決策が見つからないコードに問題があります。

void Buffer::printAllBoards()
{
    std::cout << "Total " << boards_.size() << " boards." << std::endl;
    std::map<PlayBoard, InvVertex*>::iterator itr;
    for (itr = boards_.begin(); itr != boards_.end(); ++itr)
    {
        std::cout << "the distance is " << distance(boards_.begin(), boards_.end()) << std::endl;
        //PlayBoard board = itr->first;
        //board.printBoard();
    }
}

board_ は Buffer のメンバー変数であり、型 std::map< PlayBoard, InvVertex* > を持ちます。

コードのこの部分のプログラムの出力は次のとおりです。

Total 9 boards.
the distance is 2
the distance is 2

マップに 9 つの要素を追加するため、最初の行は明らかです。しかし、反復子を使用すると、begin または end 関数のいずれかが正しい値を持たないため、そのうちの 2 つしかアクセスできません。

誰かがそれに対する解決策を持っていますか?

回答ありがとうございます。

ここの tar.gz でダウンロードとしてコードを提供しました: http://www.file-upload.net/download-7931142/invers.tar.gz.html

4

1 に答える 1

4

情報はほとんどありませんが、私が推測できる唯一の推測は、厳密な弱い順序付けPlayBoard::operator <()ではないということです。この場合、動作は未定義です。std::map

たとえば、次のコードは同様の問題を再現します。foo::operator <()完全に台無しになっていて、それが問題の原因であることに注意してください。

#include <map>
#include <iostream>

struct foo {

    foo(int i) : i{i} {}

    bool operator <(foo other) const {
        return (i != 0 || other.i != 1) && (i != 3 || other.i != 0);
    }

    int i;
};

int main() {
    std::map<foo, int> m{{0, 0}, {1, 1}, {2, 2}, {3, 3}};
    std::cout << "size = " << m.size() << '\n' <<
        "distance = " << std::distance(m.begin(), m.end()) << '\n';
}

出力は次のとおりです (GCC 4.8.1 の場合):

size = 4
distance = 3
于 2013-08-06T16:27:28.813 に答える