52 lines
1.2 KiB
C
52 lines
1.2 KiB
C
|
|
/*
|
|||
|
|
* @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
|