Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法使用函数更改数组中的元素_C++ - Fatal编程技术网

C++ 无法使用函数更改数组中的元素

C++ 无法使用函数更改数组中的元素,c++,C++,我一直在为学校做一个开发扑克游戏的项目。我有随机生成卡片的代码,但是我在使用函数对它们进行排序时遇到了问题。我相信算法本身是有效的,但我不确定如何正确访问数组中的变量。Visual Studio向我提供了类型为“int(*)[5]的参数与类型为int*(*)[5]和的参数“void sortPokerHand(int*[[5])”不兼容的错误:无法将参数1从“int[2][5]”转换为“int*[[5]” 在main()中声明扑克手 我的职能 //swap the two values void

我一直在为学校做一个开发扑克游戏的项目。我有随机生成卡片的代码,但是我在使用函数对它们进行排序时遇到了问题。我相信算法本身是有效的,但我不确定如何正确访问数组中的变量。Visual Studio向我提供了类型为“int(*)[5]的
参数与类型为
int*(*)[5]
的参数“void sortPokerHand(int*[[5])”不兼容的错误:无法将参数1从“int[2][5]”转换为“int*[[5]”

在main()中声明扑克手

我的职能

//swap the two values
void swap(int* pokerHand, int* x, int* y)
{
  int tempVal = pokerHand[0][x];
  int tempSuit = pokerHand[1][x];
  pokerHand[0][x] = pokerHand[0][y];
  pokerHand[1][x] = pokerHand[1][y];
  pokerHand[0][y] = tempVal;
  pokerHand[1][y] = tempSuit;
}

void sortPokerHand(int* pokerHand[2][5])
{
   //bubble sort poker hand
   bool swapped;
   for (int i = 0; i < 4; i++)
   {
      swapped = false;
      for (int j = 0; j < (5 - i - 1); j++)
      {
        if (pokerHand[0][j] > pokerHand[0][j + 1])
        {
            swap(pokerHand[2][5], pokerHand[0][j], pokerHand[0][j + 1]);
            swapped = true;
        }
      }

      // If no two elements were swapped by inner loop, then break 
      if (swapped == false)
        break;
   }
}

谢谢任何帮助< /P> < P>将int *PokHoal[2 ] [5 ]改为int *PokHoald。

< P>你做的比以前要难多了。考虑下面的先决条件:

  • “手”是由五个
    int
    值组成的序列
  • 只有一手牌中的牌是相对彼此排序的
因此,您的
交换
例程是完全错误的。它应该按地址使用两个
int
(因此,指向
int
)的指针),并使用它们交换内容:

void swapInt(int *left, int *right)
{
    int tmp = *left;
    *left = *right;
    *right = tmp;
}
接下来,在排序时,我们将对一只手进行排序。这意味着一个由五个
int
组成的单一序列。因此,不需要传递数组、指向数组的指针、指针数组或任何这些。只需执行此操作,干净且基本:

// assumption: a hand has five cards
void sortPokerHand(int hand[])
{
    // bubble sort sequence of int
    size_t len = 5;
    bool swapped = true;
    while (swapped && len-- > 0)
    {
        swapped = false;
        for (size_t i = 0; i < len; ++i)
        {
            if (hand[i] > hand[i + 1])
            {
                swapInt(hand + i, hand + i + 1); // note: uses our new swap function
                swapped = true;
            }
        }
    }
}
很简单。现在
main()

一如我们所料

就是这样。在更一般的解决方案中,我们会有一个任意大小的手,并且必须通过排序和打印功能使其波动,以确保完整和正确的活动。知道它是静态大小5使这变得更容易


还请注意,您可以完全更改
hands
的定义,使用指向数组的指针,而不是数组的数组,甚至是指向指针的指针,只要指向
sortHand
和/或
printHand
的对象是
int*
指向五个
int
值,它仍然可以工作。

我的问题是,首先你怎么会得到像
int*pokerHand[2][5]
这样的东西

< C++的一个优点是一个相当丰富的类型系统。如果我这么做,我可能首先定义一个卡的类型:

class card {
    enum { clubs, diamonds, spades, hearts } suit;
    int value; // 1-13 = Ace - King
public:
    bool operator<(card const &other) { 
        if (suit < other.suit)
            return true;
        if (other.suit < suit)
            return false;
        return value < other. value;
    }
};
如果您想编写自己的排序例程,您显然可以,但它仍然非常简单--一个卡片的一维向量,您只需直接进行比较,例如:

if (secondCard < firstCard) 
    swap(secondCard, firstCard);
if(第二张卡<第一张卡)
交换(第二张卡、第一张卡);

这大部分都是错误的。
int*pokerHand[2][5]
是一个由5个
int
指针组成的2个数组。
*
不应该在那里。您将
int
传递给声明接受
int*
的交换函数,然后在该函数中将其视为
int(*)[n]
,其中
n
应该是
int
,但您使用的是
int*
。简言之,没有太多不需要修复的内容。如果这没有发出编译器警告,您就需要将警告级别提高到迂腐的高度。”//气泡排序扑克手“-为什么?
std::sort有什么问题吗?
?可能仍然不起作用
int[][]
int**
不兼容,可能是因为我没有看到问题中
pokerHand
的定义<代码>整数扑克手[2][5]
当然不适合
int**
@user4581301查看问题的顶部。
void printHand(const int hand[])
{
    fputc('{', stdout);
    for (size_t i = 0; i < 5; ++i)
        printf("%d ", hand[i]);
    puts("}");
}
int main()
{
    int  hands[2][5] = 
    {
        { 5,10,7,4,1 },
        { 3,6,8,2,9 }
    };

    for (size_t i = 0; i < 2; ++i)
    {
        sortPokerHand(hands[i]);
        printHand(hands[i]);
    }

    return EXIT_SUCCESS;
}
{1 4 5 7 10 }
{2 3 6 8 9 }
class card {
    enum { clubs, diamonds, spades, hearts } suit;
    int value; // 1-13 = Ace - King
public:
    bool operator<(card const &other) { 
        if (suit < other.suit)
            return true;
        if (other.suit < suit)
            return false;
        return value < other. value;
    }
};
std::vector<card> poker_hand;
std::sort(poker_hand.begin(), poker_hand.end());
if (secondCard < firstCard) 
    swap(secondCard, firstCard);