2015-06-08 106 views
0

我想拆分TCHAR消息等給定的例子:分裂TCHAR成二維矢量

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

成二維矢量,看起來像這樣:

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個字符(地圖的每個塊)並將其保存在二維矢量上。

+0

你可以用'getchar'它得到每次一個字符。只需調用它三次。 – meneldal

回答

1

您可以使用這些下面的函數之一:

  • 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