简体   繁体   中英

C++ Function to find the min+max value element in a vector

I just started learning C++ so I am very new to this.

I have a vector vector<int> pricelist{30,10,16,25,13}; that's stored in a main class.

I want to implement a function lowestNHighestPrices() to return a Prices object that gives me the index value of the highest and lowest values in the vector (ie 0 and 1 respectively).

class Prices {
  protected:
   int lowestPrice;
   int highestPrice;

  public:
   Prices(const int lowestPriceIn, const int highestPriceIn)
       : lowestPrice(lowestPriceIn), highestPrice(highestPriceIn) {
   }

   int getlowestPrice() const {
       return lowestPrice;
   }

   int gethighestPrice() const {
       return highestPrice;
   }
};

the method will be called by this line of code Prices prices = lowestNHighestPrices(prices);

I am not entirely sure of the syntax, is there a keyword of some sort I can use with the getter methods so that I can obtain the highest and lowest values from the vector? Such that getLowestPrice() == 0 and getHighestPrice() == 1 ?

As suggested in comments, you can use std::min_element and std::max_element . The following code (with an update of the Prices class for the empty list) makes one list iteration.

class Prices {
  protected:
   int lowestPrice;
   int highestPrice;
   bool isValid;

public:
   Prices() : lowestPrice(), highestPrice(), isValid(false) {}
   Prices(const int lowestPriceIn, const int highestPriceIn)
     : lowestPrice(lowestPriceIn), highestPrice(highestPriceIn) {}
   void updateWith(int val)
     { if (!isValid) {
         lowestPrice = highestPrice = val;
         isValid = true;
       }
       else if (val < lowestPrice)
         lowestPrice = val;
       else if (val > highestPrice)
         highestPrice = val;
     }
};

Prices lowestNHighestPrices(const vector<int>& pricelist) {
  Prices result;
  for (auto val : pricelist)
    result.updateWith(val);
  return result;
}

If you have C++11 available (and you should), just use std::minmax_element :

Prices lowestNHighestPrices(const vector<int>& pricelist)
{
   assert( !pricelist.empty() ); // just in case
   auto res = std::minmax_element(
      pricelist.cbegin(),
      pricelist.cend()
   );
   return Prices( *res.first, *res.second );
}

Return a std::pair<int, int> .

std::pair<int, int> lowestNHighestPrices() const
{
   return {lowestPrice, highestPrice};
}

your class already has a public interface/methods to your high and low price values. returning another structure from it or using a pass by reference parameter is although a possible solution but looks pointless in this situation. and use of std::pair, another class/structure, should be preferred where more generic code is intended, such as containers, due to its abstract naming to access member variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM