2010-11-12 94 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,写一个解析器,并结束它用。如果你得到错误的输入,会更快,并且更容易做你想做的事。