使用vector容器的时候的错误
时间:2010-11-04 来源:mingxuan311
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<int> &rec,const hash_map<int,vector<long> > & sortBycv_map) { vector<sort_item_t> item_view_vect;
for(vector<int>::iterator iter_rec=rec.begin();iter_rec !=rec.end();++iter_rec) { sort_item_t s; s.item_id = *iter_rec; hash_map<int,vector<long> >::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<sort_item_t>::iterator iter=item_view_vect.begin();iter != item_view_vect.end(); ++iter) { rec.push_back((*iter).item_id); }
} 在写词代码的时候曾经定义了vector<sort_item_t> item_view_vect;的迭代器,然而此vector是空的,而迭代器访问的只能是非空的vector! void sortbyCV(vector<int> &rec,const hash_map<int,vector<long> > & sortBycv_map) 只能用const加引用的方式传递进去,否则整个hash_map将放到了栈上
void sortbyCV(vector<int> &rec,const hash_map<int,vector<long> > & sortBycv_map) { vector<sort_item_t> item_view_vect;
for(vector<int>::iterator iter_rec=rec.begin();iter_rec !=rec.end();++iter_rec) { sort_item_t s; s.item_id = *iter_rec; hash_map<int,vector<long> >::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<sort_item_t>::iterator iter=item_view_vect.begin();iter != item_view_vect.end(); ++iter) { rec.push_back((*iter).item_id); }
} 在写词代码的时候曾经定义了vector<sort_item_t> item_view_vect;的迭代器,然而此vector是空的,而迭代器访问的只能是非空的vector! void sortbyCV(vector<int> &rec,const hash_map<int,vector<long> > & sortBycv_map) 只能用const加引用的方式传递进去,否则整个hash_map将放到了栈上
相关阅读 更多 +