📄 function.h
字号:
/** Function.h - useful functions** Copyright (C) 2006 by Zhongjun He <zjhe@ict.ac.cn> Multilingual Interaction Technology and Evaluation Laboratory, ICT, CAS* Begin : 04/13/2006* Last Change : 04/13/2006** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU Lesser General Public* License as published by the Free Software Foundation; either* version 2.1 of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU Lesser General Public* License along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.*/
#ifndef FUNC_H
#define FUNC_H
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
/************************************************************************/
/*the map keeps n-best entries */
/************************************************************************/
template<class t1, class t2>
void insert_to_map(map<t1,t2> &m, int n, t1 v1, t2 v2)
{
map<t1, t2>::iterator it, itMin = m.begin();
if (m.find(v1) != m.end())
{
if (m[v1] < v2)
m[v1] = v2;
}
else if ((int)m.size() < n)
{
m.insert(map<t1 , t2>::value_type(v1,v2));
}
else
{
for (it=m.begin(); it!=m.end(); it++)
{
if ((*it).second < (*itMin).second)
{
itMin = it;
}
}
if (v2 > (*itMin).second)
{
m.insert(map<t1 , t2>::value_type(v1,v2));
m.erase(itMin);
}
}
}
/************************************************************************/
/* the vector keeps n-best entries */
/************************************************************************/
template<class tc>
void insert_to_vec(vector<tc> &v, int n, tc &t)
{
if (v.size() < n)
{
v.push_back(t);
}
else
{
vector<tc>::iterator it = min_element(v.begin(), v.end());
if(*it < t)
(*it) = t;
}
}
/************************************************************************/
/* pop max items */
/************************************************************************/
template<class tc>
tc popmax_from_vec(vector<tc> &v)
{
tc t;
vector<tc>::iterator it = max_element(v.begin(), v.end());
t = (*it);
v.erase(it);
return t;
}
/************************************************************************/
/* overload great */
/************************************************************************/
template<class t1,class t2>
struct greatfeat
{
bool operator()(const pair<t1,t2> &x1, const pair<t1,t2> &x2 )const
{
return (x1.second > x2.second);
}
};
/************************************************************************/
/* overload less */
/************************************************************************/
template<class t1,class t2>
struct lessfeat
{
bool operator()(const pair<t1,t2> &x1, const pair<t1,t2> &x2 )const
{
return (x1.second < x2.second);
}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -