C++;函数查找最小值+;向量中的最大值元素 我刚开始学习C++,所以我对这个很陌生。p>

C++;函数查找最小值+;向量中的最大值元素 我刚开始学习C++,所以我对这个很陌生。p>,c++,C++,我有一个向量向量价格表{30,10,16,25,13}存储在主类中 我想实现一个函数lowestNHighestPrices()来返回一个Prices对象,该对象为我提供向量中最高值和最低值的索引值(即分别为0和1) 该方法将由这行代码调用Prices Prices=lowerstnhighestprices(Prices) 我不完全确定语法,是否有某种关键字可以与getter方法一起使用,以便从向量中获得最高值和最低值?这样getLowestPrice()==0和getHighestPrice

我有一个向量
向量价格表{30,10,16,25,13}存储在主类中

我想实现一个函数
lowestNHighestPrices()
来返回一个
Prices
对象,该对象为我提供向量中最高值和最低值的索引值(即分别为0和1)

该方法将由这行代码调用
Prices Prices=lowerstnhighestprices(Prices)

我不完全确定语法,是否有某种关键字可以与getter方法一起使用,以便从向量中获得最高值和最低值?这样
getLowestPrice()==0
getHighestPrice()==1

返回一个
std::pair

std::pair lowestNHighestPrices()常量
{
返回{最低价格,最高价格};
}
返回一个
std::pair

std::pair lowestNHighestPrices()常量
{
返回{最低价格,最高价格};
}

正如注释中所建议的,您可以使用
std::min_元素
std::max_元素
。下面的代码(更新了空列表的
Prices
类)进行了一次列表迭代

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;
}
课程价格{
受保护的:
最低价格;
国际最高价格;
bool是有效的;
公众:
Prices():最低价格(),最高价格(),isValid(false){}
价格(最低价格常数,最高价格常数)
:最低价格(最低价格)、最高价格(最高价格)
void updateWith(int val)
{如果(!isValid){
最低价格=最高价格=val;
isValid=true;
}
否则如果(val<最低价格)
最低价格=val;
否则,如果(val>最高价格)
最高价格=val;
}
};
最低价格高价格(常数向量和价格表){
价格结果;
用于(自动增值:价格表)
结果:用val更新;
返回结果;
}

正如注释中所建议的,您可以使用
std::min_元素
std::max_元素
。下面的代码(更新了空列表的
Prices
类)进行了一次列表迭代

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;
}
课程价格{
受保护的:
最低价格;
国际最高价格;
bool是有效的;
公众:
Prices():最低价格(),最高价格(),isValid(false){}
价格(最低价格常数,最高价格常数)
:最低价格(最低价格)、最高价格(最高价格)
void updateWith(int val)
{如果(!isValid){
最低价格=最高价格=val;
isValid=true;
}
否则如果(val<最低价格)
最低价格=val;
否则,如果(val>最高价格)
最高价格=val;
}
};
最低价格高价格(常数向量和价格表){
价格结果;
用于(自动增值:价格表)
结果:用val更新;
返回结果;
}

您的类已经有了一个公共接口/方法,可以访问您的高价值和低价值。从中返回另一个结构或使用passby引用参数虽然是一种可能的解决方案,但在这种情况下看起来毫无意义。在需要更通用的代码(如容器)的地方,应该优先使用另一个类/结构std::pair,因为它的抽象命名可以访问成员变量

您的类已经有了一个公共接口/方法来访问您的高价格值和低价格值。从中返回另一个结构或使用passby引用参数虽然是一种可能的解决方案,但在这种情况下看起来毫无意义。在需要更通用的代码(如容器)的地方,应该优先使用另一个类/结构std::pair,因为它的抽象命名可以访问成员变量

如果您有可用的C++11(而且您应该),只需使用:

Prices lowerstnhighestprices(const vector和pricelist)
{
断言(!pricelist.empty());//以防万一
自动恢复=标准::最小最大值元素(
pricelist.cbegin(),
价格表
);
返回价格(*第1项、*第2项);
}

如果您有C++11可用(而且您应该),只需使用:

Prices lowerstnhighestprices(const vector和pricelist)
{
断言(!pricelist.empty());//以防万一
自动恢复=标准::最小最大值元素(
pricelist.cbegin(),
价格表
);
返回价格(*第1项、*第2项);
}

编写一个循环,在向量上迭代,并指定最小值和最大值。或者,如果您不关心时间,请使用
std::min_element
(max_element)抱歉,我在这里输入的代码中有一个输入错误,它意味着返回最低价格和最高价格…不确定我为什么写时间。编写一个循环,在向量上迭代,并指定最小值和最大值。或者,如果你不在乎时间,请使用
std::min_element
(max_element)抱歉,我在这里输入的代码有一个输入错误,它意味着返回最低价格和最高价格…不确定我为什么要写时间。我有C++11,但每当我尝试编译代码时,我收到一个错误,告诉我minmax_元素不是std的成员。我看不到链接。是的,我试过了,但没有成功。但我认为我不应该使用std::minmax\u元素。我必须根据.cpp“test”文件中的代码测试我的代码,我不允许更改任何内容。你看不到链接???在我的回答中,只需单击
std::minmax\u元素
。这将使您进入cppreference.com的相关页面。我真的不明白为什么你不应该使用它,因为它是最有效的解决方案。当然,这取决于你。我有C++11,但每当我尝试编译代码时,我都会收到一个错误,告诉我minmax_元素不是std的成员。我看不到链接。是的,我试过了,但没有成功。但我认为我不应该使用std::minmax\u元素。我必须根据.cpp“test”文件中的代码测试我的代码,然后
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;
}
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 );
}