我对C++非常陌生我刚刚在本周开始学习它。函数查找向量中的最小值和最大值,并返回它们的索引
如问题所说,我想在矢量vector<int> pricelist{10,23,45,12,32}
找到的最高值和最低值,所以我希望得到的返回0和2
public:
Prices(const int lowestPriceIn, const int highestPriceIn)
: lowestPrice(lowestPriceIn), highestPrice(highestPriceIn) {
}
我想提出一个功能方法lowestNHighestPrices()
。
我假设我可以创建一个遍历向量的循环,但是我将如何确定哪个是最低和最高值?我怎样才能得到索引而不是元素?
我有2个get方法返回2个int变量,一个是最低值,另一个是最高值。
int getlowestPrice() const {
return lowestPrice;
}
int gethighestPrice() const {
return highestPrice;
}
搜索如何获得矢量中的最小/最大值。然后搜索如何从迭代器获取索引。这两个人都被问及在这里回答很多次。 – juanchopanza
['std :: minmax_element'](http://en.cppreference.com/w/cpp/algorithm/minmax_element) – 101010