【发布时间】:2014-03-27 01:03:15
【问题描述】:
我想在成对的向量上调用 find 函数。在调用 find 函数时,我只有搜索的键。
我的理解是我需要将一个函数作为参数传递给 find 来为我进行比较,但我找不到合适的例子。
我在与地图容器相反的向量中对对进行排序的原因是因为我希望能够在填充过程之后按值对对进行排序。
vector< pair<string, int> > sortList;
vector< pair<string, int> >::iterator it;
for(int i=0; i < Users.size(); i++)
{
it = find( sortList.begin(), sortList.end(), findVal(Users.userName) );
//Item exists in map
if( it != sortList.end())
{
//increment key in map
it->second++;
}
//Item does not exist
else
{
//Not found, insert in map
sortList.push_back( pair<string,int>(Users.userName, 1) );
}
}
//Sort the list
//Output
findVal 上的实现对我来说是一个模糊的区域。我也愿意接受更好的实现逻辑的方法。
【问题讨论】: