【发布时间】:2015-01-19 13:39:18
【问题描述】:
我正在尝试使用WriteConsoleOutputA() 和std::vector<CHAR_INFO> 在 Win32 控制台中打印颜色模式;一切似乎都很好。但是当我尝试使用二维向量std::vector<std::vector<CHAR_INFO>> 时,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);
}
【问题讨论】:
-
与二维数组不同,二维向量的内存不能保证是连续的。
标签: c++ winapi memory console stdvector