logplus/Workflow/WFCrystal/SysUtility/utils/include/Algorithm.h
2026-01-16 17:18:41 +08:00

52 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @file Algorithm.h
*
* @date 2012-1-10
* @Author: dev
* @brief 一些通用算法
*/
#ifndef PAI_FRAME_SYSUTILITY_ALGORITHM_H
#define PAI_FRAME_SYSUTILITY_ALGORITHM_H
#include <algorithm>
#include <map>
#include "Turtle.h"
/**
* @brief 用来比较map中的值的函数对象来自于<The C++ Standard Library>
*/
template <class K, class V>
class PAI_UTILS_TEMPLATE_EXPORT value_equals
{
private:
V value;
public:
// constructor (initialize value to compare with)
value_equals (const V& v) : value(v) {}
// comparison
bool operator() (std::pair<const K, V> elem)
{
return elem.second == value;
}
};
/**
* @brief 给出Map中的Value反求对应的Key值的模板函数
* @param m 标准库map
* @param v value值
* @return value值对应的Key值如果map中不存在指定的value则返回Key类型的缺省值
*/
template <typename K,typename V,typename Map>
K MapValueToKey(const Map& m, const V& v)
{
typename Map::const_iterator pos = std::find_if(m.begin(),m.end(), value_equals<K,V>(v));
if (pos != m.end())
{
return pos->first;
}
K defaultKeyValue;
return defaultKeyValue;
}
#endif