gpt4 book ai didi

c++ - 访问 std::pair 数组的元素时出错

转载 作者:太空狗 更新时间:2023-10-29 20:17:08 25 4
gpt4 key购买 nike

我定义了一个对数组如下:

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);

但是当我尝试访问如下元素时:

delta[2].first

我收到如下错误:

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

这是我访问它的函数:

#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;
}

我哪里错了?

最佳答案

您的方法签名仅指定对单个 std::pair 实例的引用。如果您想传递一个 std::pair 数组,您可能希望您的签名看起来像下面这样。

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

关于c++ - 访问 std::pair 数组的元素时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7614683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com