向量中的分段错误

时间:2013-01-22 14:18:42

标签: c++ vector

所以,我正在从UVa Online Judge那里做着名的“The Blocks Problem"

我的方法非常愚蠢,那是因为我想玩矢量。因此,我得到了用于指向堆中每个块的指针的向量,并且这些向量存储在称为集合的向量中。

为了找到所有块,我有一个名为blockCollection的向量,其中指向所有块的指针存储在其中。

代码已通过提供的示例。我稍后会尝试编辑并提供评论。

完整来源:

#include <iostream>
#include <vector>

struct Block
{
    int id;
    std::vector<Block*>* where;
};

int positionInVector(Block* b);

int main(int argc, const char * argv[])
{
    std::vector<std::vector<Block*>*> collection;
    std::vector<Block*> blockCollection;
    std::string command = "", command2 = "";
    int blockCount = 0, k = 0, A = 0, B = 0;

    while ( std::cin >> blockCount )
    {
        std::vector<Block*>* vectors = new std::vector<Block*>[blockCount];
        Block* blocks = new Block[blockCount];
        for ( k = 0 ; k < blockCount ; ++ k)
        {
            blocks[k].id = k;
            blocks[k].where = &vectors[k];
            vectors[k].push_back(&blocks[k]);
            blockCollection.push_back(&blocks[k]);
            collection.push_back(&vectors[k]);
        }
        std::cin >> std::ws;
        while ( std::cin >> command )
        {
            if ( command == "quit" ) break;
            std::cin >> A >> command2 >> B;
            Block* blockA = blockCollection[A];
            Block* blockB = blockCollection[B];
            std::vector<Block*>* vectorA = blockA -> where;
            std::vector<Block*>* vectorB = blockB -> where;

            //exception handle
            if ( A > blockCount || B > blockCount ) continue;
            if ( A == B ) continue;
            if ( vectorA == vectorB ) continue;

            if ( command == "move" )
            {
                //move anything on top of A to its original position
                int positionOfBlockAInVectorA = positionInVector(blockA);
                for ( int i = positionOfBlockAInVectorA + 1 ; i < vectorA -> size() ; ++ i )
                {
                    Block* blockToBeMoved = *(vectorA -> begin() + i);
                    std::vector<Block*>* destinationVector = collection[blockToBeMoved -> id];
                    blockToBeMoved -> where = destinationVector;
                    destinationVector -> push_back(blockToBeMoved);
                }
                vectorA -> erase(vectorA -> begin() + positionOfBlockAInVectorA + 1, vectorA -> end());
            }
            if ( command2 == "onto" )
            {
                //move anything on top of B to its original position
                int positionOfBlockBInVectorB = positionInVector(blockB);
                for ( int i = positionOfBlockBInVectorB + 1 ; i < vectorB -> size() ; ++ i )
                {
                    Block* blockToBeMoved = *(vectorB -> begin() + i);
                    std::vector<Block*>* destinationVector = collection[blockToBeMoved -> id];
                    blockToBeMoved -> where = destinationVector;
                    destinationVector -> push_back(blockToBeMoved);
                }
                if (positionOfBlockBInVectorB + 1 > vectorB -> size()) vectorA -> erase(vectorB -> begin() + positionOfBlockBInVectorB + 1, vectorB -> end());
            }
            if ( command == "move" )
            {
                //move block a to the pile containing block b
                vectorA -> pop_back();
                blockA -> where = vectorB;
                vectorB -> push_back(blockA);
            }
            else
            {
                //move block a and those on top of it to the pile containing block b
                std::vector<Block*> temperaryVector;
                int positionOfBlockAInVectorA = positionInVector(blockA);
                for ( int i = (int)vectorA -> size() - 1 ; i >= positionOfBlockAInVectorA ; -- i )
                {
                    temperaryVector.push_back(vectorA -> at(i));
                    vectorA -> erase(vectorA -> begin() + i);
                }
                for ( int i = (int)temperaryVector.size() - 1 ; i >= 0 ; -- i )
                {
                    temperaryVector[i] -> where = vectorB;
                    vectorB -> push_back(temperaryVector[i]);
                }
            }
        }

        for ( k = 0 ; k < blockCount ; ++ k )
        {
            std::vector<Block*>* vector = collection[k];
            std::cout << k << ":";
            if ( !vector -> empty() )
            {
                for ( int i = 0 ; i < vector -> size() ; ++ i )
                {
                    std::cout << " " << vector -> at(i) -> id;
                }
            }
            std::cout << std::endl;
        }
        delete [] blocks;
        delete [] vectors;
    }
    return 0;
}

int positionInVector(Block* block)
{
    std::vector<Block*> vector = *block -> where;
    for ( int i = 0 ; i < vector.size() ; ++ i )
    {
        if ( vector[i] == block ) return i;
    }
    return -1;
}

谢谢!

2 个答案:

答案 0 :(得分:1)

每次向blockCollection添加或删除块时,您对集合中任何Block所持的每个指针都可能无效。

我认为这是我最初需要说的......

答案 1 :(得分:1)

为此起作用:

        A = (int)command[5] - 48;
        B = (int)command[12] - 48;

我们需要确保字符串长度为5/12个字符,并且这些位置中有一个数字。代码应添加对输入长度和这些位置中数字有效性的检查。