我有一个像这样的地图,枚举为关键,UINT为值.
#include <iostream>
#include <string>
#include <map>
using namespace std;
typedef enum tcGroup_t
{
CG_NONE = 0,
CG_BASE = 1,
CG_RC = 3,
CG_HTD = 4,
CG_HID = 5
} tcGroup;
int getMaxtcGroupCount()
{
std::map<tcGroup, UINT> maxTcGroup;
maxTcGroup [CG_BASE] = 2;
maxTcGroup [CG_HID] = 33;
maxTcGroup [CG_HTD] = 44;
maxTcGroup [CG_RC] = 87;
maxTcGroup [CG_NONE] = 39;
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想将地图中的最高值返回给调用函数.在上面的例子中,我想返回值87.我知道地图是通过Key排序的,但在我的情况下,我想要返回地图中的最高值?
任何帮助表示赞赏.谢谢
您可以使用std::max_element合适的仿函数.
bool cmp(const std::pair<const tcGroup, UINT>& rhs,
const std::pair<const tcGroup, UINT>& lhs)
{
return rhs.second < lhs.second;
}
Run Code Online (Sandbox Code Playgroud)
然后
auto max_iter = max_element(maxTcGroup.begin(), maxTcGroup.end());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |