C++ std::cout在我的程序运行到一半时无法打印整数
我是。。。所以很困惑C++ std::cout在我的程序运行到一半时无法打印整数,c++,C++,我是。。。所以很困惑 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){ blo
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;
}
}
在停止之前只打印了几张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;
//只导致对基础文件的一次写入
//而不是5,这是更好的
//表演。
std::cout记得std::cout
的输出已完全缓冲。如果实际打印的输出与您的操作位置无关,例如,从您的代码判断,您很有可能遇到内存访问冲突。使用调试器找出哪里或哪里出了问题。另外std::cout完全脱离主题:“A*”通常读作“星”,而不是“尖”。您的“开放列表”从一个元素开始,您永远不会再添加任何内容。do
-循环的第一次迭代只删除该元素。在第二次迭代中,永远不会进入搜索循环。繁荣
closedList[x][y] = false;
maze[x/BLOCK_SIZE][y/BLOCK_SIZE] = new_block;
}
std::cout << 10;
}
// Causes only one write to underlying file
// instead of 5, which is much better for
// performance.
std::cout << a << " + " << b << " = " << std::endl;
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " " << flush;
this_thread::sleep_for(chrono::seconds(1));
}
return 0;
}