2010-11-12 104 views
0

快速問題在這裏。我想知道如何從用戶輸入創建2D矢量。對於一個項目,我將我的「棋盤」存儲爲2D矢量,並且用戶將輸入它的高度和寬度,以及或許是的起始配置。從C++輸入創建2D矢量

如果我的主板存儲爲:

vector<vector<int> > myBoard(width, vector<int> (height)); 
//Not sure how to get width and height from input... 

我需要將其初始化到給定參數(如果用戶提供的信息),填寫板附片。用戶將通過1 cin在1行輸入所有這些信息。所以像這樣...

Please type the board config: 3 3 

Please type the board config: 3 3 . . . . . . X . O 

Please type the board config: 3 3 ABFDFL($%$ 

隨着最後一個是壞的輸入的一個例子。第一個例子會創建一個2D矢量,3乘3。第二個例子會創建一個2D矢量,3乘3,然後用指定的位置填充棋盤。在這種情況下, 」。」是0,「X」是1,「O」是-1。 這是我遇到最多麻煩的部分。我可以把它保存爲一個字符串,但似乎經歷和解析這將是一個痛苦的對接......

+0

對此的解析聽起來微不足道......確切的問題是什麼? – 2010-11-12 00:22:29

回答

2

我會嘗試從用戶輸入中推斷出尺寸。

一個例子會話:

A board consists of characters ., X or O. 
An example board: 
.XO 
... 
X.. 

Enter the board, end with two Returns: 
..... 
..XO. 
.XXX. 
OOOOO 
..... 

然後你將掃描的第一行找到寬度,檢查每一行有效字符和相同的寬度,並計算行找到的高度。

0

我可以把它存儲到字符串,但是它 似乎經歷和解析 這將是一個痛苦的屁股...

爲什麼?這是一個單一的if/else鏈或switch

遲早你會厭倦管理向量載體。我會把整個板子放在一個單獨的矢量中,並解決它y * width + x。它甚至可能成爲未來的一類:

Piece board::getPieceAt(int x, int y) const 
{ 
    ... 
} 
+0

異常規範不僅是不好的做法,而且在C++ 0x中已棄用。 – GManNickG 2010-11-12 00:25:23

+0

已刪除。那是因爲他們不能保證異常安全? – aib 2010-11-12 00:36:56

+0

異常規範有性能問題,比他們的價值更麻煩。請參閱http://www.gotw.ca/gotw/082.htm進行良好的討論。 – beldaz 2010-11-12 00:56:30

1

您可以使用一個地圖,其中的關鍵字是從cin讀取的棋盤座標的std :: pair作爲整數。值表示可以是int或char,並且很容易訪問。然後你可以做類似的事情...

if(board[std::pair(x,y)] == '.') 
    //do something; 
1

解析它已經,看在上帝的份上!如果你允許的只是'。','X'和'O',那麼基本上你所要做的就是跳過空格。 (如果你喜歡你的用戶,你可能會考慮允許使用小寫字母。)

0

可能導致方形輪的僞代碼。

getline(cin, board); 

find location of first space: board.find(' '); 
assign this to size_t height 
extract the digit(s) to a sting (or vector?): board.substr(0, height) 
atoi this string for the height 
cut this from the string: board = board.substr(height) 
repeat for width 
initialize the realBoard vector with width and height 

if board still has chars, start filling the board 
initialize two ints for the vector coordinates 
initialize another for putting numbers onto the board 

until you get to the end of board 
send board[ i ] to a switch 
as you said, o/O = -1; x/X = 1; ./default = 0 
(unless you want bad input to interrupt execution) 
put the value into realBoard[ h ][ w ] 
add one to w; 
if it is above width, make it 0, add one to h 
if h > height, stop this function 
0

下面是使用CIN及其機制的方法:

#include <stdio.h> 
#include <iostream> 
#include <vector> 
#include <limits> 
#include <ctype.h> 


using namespace std; 


int main(){ 
    int x; 
    int y; 
    bool done = false; 

    vector<int> inputs; 
    char holdValue; 


    while (!done) 
    { 
     inputs.clear(); 
     cout << "Enter Data:" << endl; //something more descriptive then then this 
     cin >> x >> y; 
     if(cin.fail()) 
     { 
      cin.clear(); 
      cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
      cout << "Error with your input" << endl; 
      continue; 
     } 

     int i = 0; 
     bool error = false; 
     for (; i < (x * y) && error != true ; ++i) 
     { 
      cin >> holdValue; 
      if(cin.fail()) 
      { 
       cin.clear(); 
       cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
       cout << "Error with your input" << endl; 
       error = true; 
      } 
      switch(toupper(holdValue)) 
      { 
       case 'X': 
        inputs.push_back(1); 
        break; 

       case 'O': 
        inputs.push_back(-1); 
        break; 

       case '.': 
        inputs.push_back(0); 
        break; 

       default: 
        cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n'); 
        cout << "Error with your input" << endl; 
        error = true; 
        break; 
      } 
     } 

     if(i >= (x * y) && error != true) 
     { 
      done = true; 
     } 
    } 

    //At this piont you have a vector to do with as you please 

} 

但FOT的努力,量你還不如讀入一個的std :: string,寫一個解析器,並結束它用。如果你得到錯誤的輸入,會更快,並且更容易做你想做的事。