Error when attempting to use max_element to figure out the highest number in an array

帅比萌擦擦* 提交于 2020-01-30 13:17:48

问题


int a = max_element(highesthuman[0], highesthuman[2]); 
  if( win > loss)
  {
    cout << "You won " << (win-loss) << " games more than the computer did! You used " << a  << "     the most."; 
  }
}

The above array is given by

int humanrock = 0;
int humanpaper = 0;
int humanscissors = 0;
int highesthuman [3] = {humanrock, humanpaper, humanscissors};

When running my whole program I get an error saying "invalid type argument of unary". I looked this up but was not able to understand "pointers" or what people were referring too.


回答1:


std::max_element() accepts two iterators as parameters, and returns an iterator. In your case iterators are pointers. So you should change

int a = max_element(highesthuman[0], highesthuman[2]); 

to

int a = *max_element(highesthuman, highesthuman + 3); 


来源:https://stackoverflow.com/questions/25964349/error-when-attempting-to-use-max-element-to-figure-out-the-highest-number-in-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!