c++ - 将 TCHAR 拆分为二维 vector

标签 c++ winapi

我想像给定的例子那样拆分一条 TCHAR 消息:

TCHAR [1000] = "[X][X]   [X][X][X]   [X][P][-]..."

变成一个二维 vector ,看起来像这样:

enter image description here

void Comunnication::receiveMessage(tstring msg){

    TCHAR gameMessage[1000];
    vector<vector<tstring>> gameMap;

    BOOL isSucceed = ReadFile(serverUpdatePipe, gameMessage, sizeof(gameMessage), &bytesRead, NULL);

    gameMessage[bytesRead / sizeof(TCHAR)] = '\0';

    if (!isSucceed || !bytesRead){
        break;
    }

    //Wrong 
    for (DWORD i = 0; i < 15; i++)
    {
        vector<tstring> line;
        for (DWORD j = 0; j < 15; j++)
        {
            // get 3 charaters of each time
        }
        communication.getMap().push_back(line);
    }

}

这里的问题是我不知道如何获取 3 个字符( map 的每个 block )并将其保存在二维 vector 上。

最佳答案

您可以使用以下功能之一:

  • std::istream::get

您可以在 here 中阅读有关此功能的信息,istream& get (char* s, streamsize n);.

例子:

char *s;
cin.get(s, 4);
cout << s << endl;

和输出:

string test
str
  • std::istream::read

或者你可以使用这个函数,istream& read (char* s, streamsize n);here中的描述. 如果使用此功能,必须先定义行的大小为3。

例子:

char s[3];
cin.read(s, 3);
cout << s << endl;

和输出:

string test
str

关于c++ - 将 TCHAR 拆分为二维 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30700085/

相关文章:

C++ 返回后这是一个 "invalid"引用吗?

c++ - Visual Studio 2013 : CL. exe 已退出,代码为 -1073741515

c++ - QML无框窗支持aero snap

c++ - VC++ win32 API编程 : how can I get the image out of the clipboard and display it in a windows?

c++ - 在 ResizeToContents 模式下调整大小时,QHeaderView 仅考虑当前屏幕上的项目

c++ - 尝试在 C++ 中打开多个文件时出错

c++ - SFML 2.1 复制图像

c++ - 什么时候应该调用 SelectFont?

c++ - GetDIBits() 返回错误的 BGR 值 :

winapi - 窃取焦点,因为 SetForegroundWindow 做不到