簡體   English   中英

從C ++輸入創建2D向量

[英]Create 2D vector from input in C++

在這里快速提問。 我想知道如何根據用戶輸入創建2D向量。 對於一個項目,我存儲我的“板”作為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($%$

最后一個是輸入錯誤的示例。 第一個示例將創建3 * 3的2D矢量。第二個示例將創建3 * 3的2D矢量,並使用給定位置填充木板。 在這種情況下, ”。” 是0,“ X”是1,“ O”是-1。 那是我最麻煩的部分。 我可以將其存儲到字符串中,但是似乎要經歷並解析它的過程會很麻煩……

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

會話示例:

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

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

然后,您將掃描第一行以找到寬度,檢查每行中是否有有效字符和相同寬度,然后對行進行計數以找到高度。

您可以使用鍵為std :: pair且從cin讀取為整數的板坐標的映射。 值表示形式可以是int或char,並且可以輕松訪問。 然后您可以做...

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

為了天堂的緣故,已經解析了! 如果您只允許'。','X'和'O',那么基本上您要做的就是跳過空格。 (如果您喜歡用戶,則可以考慮允許使用小寫字母。)

我可以將其存儲到字符串中,但是似乎要經歷並解析它的過程會很麻煩……

為什么? 它是單個if / else chain或switch

遲早您會厭倦管理向量的向量的。 我將整個電路板放在一個向量中,並處理為y * width + x 將來它甚至可能成為自己的一類:

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

偽代碼可能導致方輪。

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  

這是使用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

}

但是,經過如此多的努力,您最好將其讀入std :: string並編寫一個解析器,然后再使用它。 如果輸入錯誤,可以更快,更輕松地進行所需的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM