struct sort_item_t
{
int item_id;
int item_viewcount;
};
bool operator<(const sort_item_t &sa,const sort_item_t &sb)
{
return sa.item_viewcount > sb.item_viewcount;
}
void sortbyCV(vector &rec,const hash_map > & sortBycv_map)
{
vector item_view_vect;
for(vector::iterator iter_rec=rec.begin();iter_rec !=rec.end();++iter_rec)
{
sort_item_t s;
s.item_id = *iter_rec;
hash_map >::const_iterator //只能用const迭代器iter_map=sortBycv_map.find(*iter_rec);
if(iter_map != sortBycv_map.end())
{
s.item_viewcount = (iter_map->second).size();
}
else
{
s.item_viewcount = 0;
}
item_view_vect.push_back(s);
}
sort(item_view_vect.begin(),item_view_vect.end());
rec.clear();
//now the vector (item_id,item_viewcount) is sort pushback the item_id in rec and return
for(vector::iterator iter=item_view_vect.begin();iter != item_view_vect.end();
++iter)
{
rec.push_back((*iter).item_id);
}
}
在写词代码的时候曾经定义了vector item_view_vect;的迭代器,然而此vector是空的,而迭代器访问的只能是非空的vector!
void sortbyCV(vector &rec,const hash_map > & sortBycv_map)
只能用const加引用的方式传递进去,否则整个hash_map将放到了栈上
阅读(1462) | 评论(0) | 转发(0) |