我有以下简化的文件和类:
统计:
class Stat
{
auto getMinMaxValue(std::unordered_map< int, int >&);
};
统计.cpp:
auto Stat::getMinMaxValue(std::unordered_map< int, int >&m)
{
return std::minmax_element(m.begin(), m.end(), [](const pair<int, int>& p1, const pair<int, int>& p2) { return p1.second < p2.second; });
}
StatCount.h:
class StatCount : public Stat
{
void setWeight(std::vector<D> const&, const std::string);
};
统计计数.cpp:
void StatCount::setWeight(vector<D> const& ref, const string type)
{
auto a = Stat::getMinMaxValue(m_value);
cout << "MIN: " << a.first->second << endl;
cout << "MAX: " << a.second->second << endl;
}
由于如果我使用自动返回类型,我将函数"getMinMaxValue"声明到基类 Stat 中,因此出现错误:
function 'getMinMaxValue' with deduced return type cannot be used before it is defined
但我未能删除自动返回类型并找到正确的语法来指定方法"getMinMaxValue"的返回类型
如果我阅读有关 CPP首选项的文档,我看到它一定是一对迭代器,但是如何?
我回应自己,似乎我已经找到了解决方案:
统计:
std::pair<std::unordered_map< int, int >::iterator, std::unordered_map< int, int >::iterator> getMinMaxValue(std::unordered_map< int, int >&);