max_element() 给出非常奇怪的错误消息

max_element() is giving very weird error messages

本文关键字:错误 消息 非常 element max      更新时间:2023-10-16

在学习 c++ 时,我正在尝试制作一个石头剪刀布机器人(以一种非常愚蠢的方式(可能会发现对方玩家的任何模式。

#include <iostream>
#include <array>
using namespace std;
int check_winner(int a, int b) {
if (a == b) {
return 2;
}
else if (b - a == 1) {
return 1;
}
else if (b - a == -2) {
return 1;
}
else {
return 0;
}
}
int main() {
int bot_choice = rand() % 3 + 1;
int user_choice = 0;
bool run = true;
array<int, 3> rocks = {0,0,0};
array<int, 3> papers = {0,0,0};
array<int, 3> scissors = {0,0,0};
int last_choice = 0;
while (run == true) {
if (last_choice == 0) {
bot_choice = std::distance(rocks.begin(), std::max_element(rocks,rocks.end()));
}
cout << "Enter your choice from 1 to 3, or enter 0 for exiting ";
cin >> user_choice;
system("read");
if (user_choice == 0) {
return 0;
}
else {
if (last_choice == 0) {
rocks[user_choice] += 1;
}
else if (last_choice == 1) {
papers[user_choice] += 1;
}
else {
scissors[user_choice] += 1;
}
last_choice = user_choice;
cout << "Bot chose " << bot_choice << "n";
if (check_winner(user_choice,bot_choice) == 0) {
cout << "User won!n";
}
else if (check_winner(user_choice,bot_choice) == 1) {
cout << "Bot won!n";
}
else {
cout << "Draw!n";
}
}
}    
}

每当我尝试运行代码时,都会出现以下错误消息:

learning_rps.cpp:30:55: error: no matching function for call to 'max_element'
bot_choice = std::distance(rocks.begin(), std::max_element(rocks,rocks.end()));
^~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2584:1: note: candidate template ignored: deduced conflicting types for parameter '_ForwardIterator'
('std::__1::array<int, 3>' vs. 'int *')
max_element(_ForwardIterator __first, _ForwardIterator __last)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2565:1: note: candidate function template not viable: requires 3 arguments, but 2 were provided
max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
^
1 error generated.

但是,如果我在用于测试的max_element参数中添加一个零,则会出现另一个错误,在我看来,这是完全不合逻辑的:

learning_rps.cpp:30:55: error: no matching function for call to 'max_element'
bot_choice = std::distance(rocks.begin(), std::max_element(rocks,rocks.end(),0));
^~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2565:1: note: candidate template ignored: deduced conflicting types for parameter '_ForwardIterator'
('std::__1::array<int, 3>' vs. 'int *')
max_element(_ForwardIterator __first, _ForwardIterator __last, _Compare __comp)
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:2584:1: note: candidate function template not viable: requires 2 arguments, but 3 were provided
max_element(_ForwardIterator __first, _ForwardIterator __last)

所以基本上如果我提供 2 个参数,它说我需要 3,但如果我提供 3,它说我需要 2。我对 c++ 非常陌生,所以欢迎任何关于为什么这是错误的解释以及任何其他优化。提前感谢!

你需要做:

bot_choice = std::distance(rocks.begin(), std::max_element(rocks.begin(),rocks.end()));

请参阅以下示例部分: https://en.cppreference.com/w/cpp/algorithm/max_element