gpt4 book ai didi

c++ - vector 中的段错误

转载 作者:行者123 更新时间:2023-11-28 03:24:44 27 4
gpt4 key购买 nike

所以,我正在做来自 UVa Online Judge 的著名的“The Blocks Problem"”。

我的方法很愚蠢,那是因为我想玩 vector 。所以,我得到了指向堆中每个 block 的指针的 vector ,这些 vector 存储在一个称为集合的 vector 中。

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

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

完整来源:

#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;
}

谢谢!

最佳答案

为了让它工作:

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

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

关于c++ - vector 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14460673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com