C++ 具有WriteConsoleOutputA的怪异输出
我正在使用C++ 具有WriteConsoleOutputA的怪异输出,c++,winapi,memory,console,stdvector,C++,Winapi,Memory,Console,Stdvector,我正在使用WriteConsoleOutputA()和std::vector在Win32控制台中打印颜色图案;一切似乎都很好。但是,当我尝试使用二维向量时,std::vectorWrtieConsoleOutputA()。我不知道代码中的错误在哪里 这是我的密码: #include <ctime> #include <Windows.h> #include <vector> int main() { srand((unsigned)time(NULL
WriteConsoleOutputA()
和std::vector
在Win32控制台中打印颜色图案;一切似乎都很好。但是,当我尝试使用二维向量时,std::vector
WrtieConsoleOutputA()。我不知道代码中的错误在哪里
这是我的密码:
#include <ctime>
#include <Windows.h>
#include <vector>
int main()
{
srand((unsigned)time(NULL));
const int width = 80, height = 25;
COORD charBufferSize{ width, height };
COORD characterPosition{ 0, 0 };
SMALL_RECT writeArea{ 0, 0, width - 1, height - 1 };
std::vector<std::vector<CHAR_INFO>> backBuffer(height, std::vector<CHAR_INFO>(width));
for (auto& i : backBuffer)
{
for (auto& j : i)
{
j.Char.AsciiChar = (unsigned char)219;
j.Attributes = rand() % 256;
}
}
WriteConsoleOutputA(GetStdHandle(STD_OUTPUT_HANDLE), backBuffer[0].data(), charBufferSize, characterPosition, &writeArea);
}
#包括
#包括
#包括
int main()
{
srand((无符号)时间(NULL));
常数int宽度=80,高度=25;
坐标大小{宽度,高度};
坐标字符位置{0,0};
小直写区{0,0,宽-1,高-1};
标准::向量backBuffer(高度,标准::向量(宽度));
用于(自动和i:backBuffer)
{
对于(auto&j:i)
{
j、 Char.ascichar=(无符号字符)219;
j、 属性=rand()%256;
}
}
WriteConsoleOutputA(GetStdHandle(STD_OUTPUT_HANDLE),backBuffer[0]。data(),charBufferSize,characterPosition,&writeArea);
}
问题在于嵌套的std::vector
s的内存分配布局,以及它与Win32 APIWriteConsoleOutput()
所期望的不匹配
std::vector
连续分配其内存。但是如果在外部std::vector
中嵌套了std::vector
,那么整个分配的内存就不再是连续的了
如果需要一个完整的连续内存块,则应分配一个总大小为宽度x高度的std::vector
,并将其用作writeconoleoutput()
的内存缓冲区
我按照以下路径稍微修改了您的代码,现在它似乎可以工作了:
#include <ctime>
#include <Windows.h>
#include <vector>
int main()
{
srand((unsigned)time(NULL));
const int width = 80;
const int height = 25;
COORD charBufferSize{ width, height };
COORD characterPosition{ 0, 0 };
SMALL_RECT writeArea{ 0, 0, width - 1, height - 1 };
//
// NOTE:
//
// Wrong memory layout: vector<vector<...>> is *not* contiguous as a whole
//
// std::vector<std::vector<CHAR_INFO>> backBuffer(height,
// std::vector<CHAR_INFO>(width));
//
//
// Correct memory layout: allocate a single *contiguous* block
// of memory, to store a 2D array of width x height
//
std::vector<CHAR_INFO> backBuffer(width * height);
//
// Iterate through the backBuffer items
// as if it were a 2D array of size width x height
//
for (size_t row = 0; row < height; ++row)
{
for (size_t col = 0; col < width; ++col)
{
CHAR_INFO& curr = backBuffer[row*width + col];
// Your previous code
curr.Char.AsciiChar = static_cast<unsigned char>(219);
curr.Attributes = rand() % 256;
}
}
WriteConsoleOutputA(GetStdHandle(STD_OUTPUT_HANDLE),
backBuffer.data(),
charBufferSize,
characterPosition,
&writeArea);
}
#包括
#包括
#包括
int main()
{
srand((无符号)时间(NULL));
const int width=80;
const int height=25;
坐标大小{宽度,高度};
坐标字符位置{0,0};
小直写区{0,0,宽-1,高-1};
//
//注:
//
//错误的内存布局:向量作为一个整体是*不*连续的
//
//标准::矢量反向缓冲(高度,
//std::矢量(宽度);
//
//
//正确的内存布局:分配单个*连续*块
//存储一个宽x高的2D数组
//
标准::矢量反向缓冲(宽度*高度);
//
//遍历backBuffer项
//就好像它是一个大小为宽x高的二维数组
//
用于(行大小=0;行<高度;++行)
{
用于(大小列=0;列<宽度;++列)
{
CHAR_INFO&curr=backBuffer[行*宽+列];
//您以前的代码
curr.Char.ascichar=静态(219);
curr.Attributes=rand()%256;
}
}
WriteConsoleOutputA(GetStdHandle(标准输出句柄),
backBuffer.data(),
字符大小,
角色位置,
&书面形式(a);
}
与二维数组不同,二维向量的内存不能保证连续。