繁体   English   中英

std::cout 无法在我的程序中途打印整数

[英]std::cout fails to print integers halfway through my program

我……太糊涂了。

void CreateMaze(){
//Creates the maze
sf::Vector2f start = StartingPoint();
//Sets information for each node "block"
for (int x = 0; x < WIDTH; x += BLOCK_SIZE){
    for (int y = 0; y < HEIGHT; y += BLOCK_SIZE){
        block new_block;

        ///block.shape info
        sf::RectangleShape rec;
        rec.setSize(sf::Vector2f(BLOCK_SIZE, BLOCK_SIZE));
        rec.setPosition(x, y);

        //Creates basic grid
        if (PartOfCircle(x, y)){
            rec.setFillColor(black);
        }else{

            rec.setFillColor(white);
        }
        if (PartOfCenter(x, y)){
            rec.setFillColor(red);
        }

        //Starting point
        if (rec.getPosition() == start){
            rec.setFillColor(green);
            greenPos.x = x/BLOCK_SIZE;
            greenPos.y = y/BLOCK_SIZE;
        }
        new_block.shape = rec;
        ///block costs and parent position info
        new_block.fCost = FLT_MAX;
        new_block.gCost = FLT_MAX;
        new_block.hCost = FLT_MAX;
        new_block.pos = sf::Vector2i(x/BLOCK_SIZE, y/BLOCK_SIZE);
        new_block.parentPos = sf::Vector2i(-1, -1);
        closedList[x][y] = false;

        maze[x/BLOCK_SIZE][y/BLOCK_SIZE] = new_block;
    }
}

我正在使用 A* 算法实现迷宫求解器。 这是创建程序生成迷宫的函数的一部分(不是很复杂,但我尽力了)。 代码完全按照它应该的方式工作。

一段时间后,在实现 A* 算法时,我偶然发现了一个问题。

std::vector<block> ASharp(){
//Uses A* algorithm to find the fastest solution through the maze
std::vector<block> openList;
std::cout << openList.size();
sf::Vector2i curPos = greenPos;
maze[curPos.x][curPos.y].fCost = 0.0;
maze[curPos.x][curPos.y].gCost = 0.0;
maze[curPos.x][curPos.y].hCost = 0.0;
maze[curPos.x][curPos.y].parentPos = curPos;
openList.emplace_back(maze[curPos.x][curPos.y]);
while (!openList.empty() && openList.size() < (WIDTH/BLOCK_SIZE) * (HEIGHT/BLOCK_SIZE)){
    block route;
    do{
        float tmp = FLT_MAX;
        std::vector<block>::iterator itBlock; //Used to erase the correct pos

        //Find the block in openList that has the least f
        for (std::vector<block>::iterator it = openList.begin();
            it != openList.end(); it = next(it)) {
            std::cout << openList.size();
            block n = *it;
            if (n.fCost < tmp) {
                tmp = n.fCost;
                itBlock = it;
            }
        }
        route = *itBlock;
        openList.erase(itBlock);
    }while(!SquareIsValid(route.shape.getFillColor()));

这是我实施的一部分。

尝试运行我的程序会导致在route = *itBlock处崩溃。 我真的不知道这是否与我在这里询问的问题有关,但它可能是相关的,所以我想我会添加它。

你看,问题是在试图找出导致 ASharp() 崩溃的原因时,我使用 std::cout 来输出一些信息。 奇怪的是,在 main{} 中调用 CreateMaze() 后会打印字符串,而不是整数。 在调用 CreateMaze 之前,它会打印得很好。

我经历了 CreateMaze 并开始输入std::cout << 10; 在各个地方尝试追踪开始失败的地方。 看起来,这是在 x 循环中途停止打印。

        closedList[x][y] = false;

        maze[x/BLOCK_SIZE][y/BLOCK_SIZE] = new_block;
    }
    std::cout << 10;
}

在停止之前只打印几个 10...

我真的不明白为什么或什至会发生这样的事情。 如果有人有任何想法,请告诉我。 提前致谢。

在 C++ 中,我们可以显式刷新以强制写入缓冲区。 通常 std::endl 函数通过插入换行符并刷新流来工作。 stdout/cout 是行缓冲的,即在您编写换行符或显式刷新缓冲区之前,输出不会发送到操作系统。 例如,

// Causes only one write to underlying file  
// instead of 5, which is much better for  
// performance. 
std::cout << a << " + " << b << " = " << std::endl; 

另一种方法是使用 std::flush ,顾名思义,它可以刷新缓冲区。 例如:

using namespace std; 
int main() 
{ 
   for (int i = 1; i <= 5; ++i) 
   { 
      cout << i << " " << flush; 
      this_thread::sleep_for(chrono::seconds(1)); 
   } 
   return 0; 
} 

将一个接一个地打印每个数字,而不是一次。 所以我建议修复你的 std::cout 并添加 std::endl 来刷新缓冲区。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM