开发者

error accessing elements of array of std::pair

开发者 https://www.devze.com 2023-04-09 10:17 出处:网络
I have defined an array of pairs as following: std::pair<int,int> delta[4]; delta[0] = std::make_pair(0,1);

I have defined an array of pairs as following:

std::pair<int,int> delta[4];
delta[0] = std::make_pair(0,1);
delta[1] = std::make_pair(1,1);
delta[2] = std::make_pair(1,0);
delta[3] = std::make_pair(1,-1);

but when I try to access the elements like:

delta[2].first

I get an error as following :

binary '[' : 'std::pair<_Ty1,_Ty2>' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2228: left of '.first' must have class/struct/union
开发者_如何学Go

Here is the function where I access it:

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>

int who_won(std::pair<int,int> &delta,std::vector<std::vector<int>> &grid);

int main(int argc, char** argv)
{
    std::fstream input;
    input.open("E:\\in.txt",std::ios_base::in);
    std::string line;
    char ch;
    std::vector<std::vector<int>> grid;
    std::vector<int> row;
    while(input.get(ch))
    {
        if(ch == '\n')
        {
            grid.push_back(row);
            row.clear();
        }
        else if(ch != ',')
        {
            row.push_back(std::atoi(&ch));
        }
    }

    if(row.size()>0)
        grid.push_back(row);
    row.clear();

    std::pair<int,int> delta[4];
    delta[0] = std::make_pair(0,1);
    delta[1] = std::make_pair(1,1);
    delta[2] = std::make_pair(1,0);
    int l = grid.size();
    for( int i =0; i < l; ++i)
    {
        //auto & v = *i;
        for( int j = 0; j < l; ++j)
            std::cout<<grid[i][j]<<" ";
        std::cout<<"\n";
    }

    int winner = who_won(*delta,grid);
    if( winner == 0)
    {
        if( std::find(grid.front().begin(),grid.front().end(),0) == grid.front().end())
            std::cout<<"draw";
        else
            std::cout<<"play";
    }
    else if( winner == 1)
        std::cout<<"1";
    else if( winner == 2)
        std::cout<<"2";


    return 0;
}

int who_won(std::pair<int,int> delta[],std::vector<std::vector<int>> &grid)
{
    int l = grid.size(),connect;
    for(int px = 0; px < l; ++px)
    {
        for(int py = 0; py < l; ++py)
        {

            for(int i = 0;i < 4;++i)
            {
                connect = 0;
                for(int j = 1;j < 4;++j)
                {
                    if( grid[px + delta[i].first*j][py + delta[i].second*j] != grid[px][py])
                        break;
                    else
                        connect++;
                }
                if(connect == 4)
                    return grid[px][py];
            }
        }
    }
    return -1;
}

Where am I going wrong ?


You method signature only specifies a reference to a single std::pair instance. You would probably want your signature to look something like the following if you want to pass an array of std::pair.

int who_won(std::pair<int,int> delta[],std::vector<std::vector<int>> &grid)
0

精彩评论

暂无评论...
验证码 换一张
取 消