#include "UserUserSim.h"
UserUserSim::UserUserSim(string &query_url):
_query_url(query_url)
{
}
void UserUserSim::calculate(){
ifstream infile(_query_url.c_str());
string line;
while(infile){
int movie_id;
int user_id;
infile>>line;
if (line[line.length()-1]==':'){
movie_id=atoi(line.c_str());
cout<<line<<endl;
}
else{
user_id=atoi(line.c_str());
if (_set.find(user_id)==_set.end())
getTop(user_id);
float score=getScore(user_id,movie_id);
cout<<score<<endl;
}
}
}
float UserUserSim::getScore(int &user_id, int &movie_id){
vector<USim>* p=_map[user_id];
MovieList* ml=MovieDictionary::getInstance().getMovie(movie_id);
ml->sortList();
vector<UserScore>::iterator it;
vector<USim>::iterator sim_it=p->begin();
float score=0;
float total_weight=0;
for (it=ml->begin();it<ml->end();it++){
while ((*it).user_id>(*sim_it).user_id){ // the user did not rate in ths movie
sim_it++;
}
if ((*it).user_id==(*sim_it).user_id){
score+=(*sim_it).score * (*it).rating; // score of similarity * rating
total_weight+=(*sim_it).score;
sim_it++; // move on to the next user
}
}
score=score/total_weight;
return score;
}
typedef pair<int, float> mapPair;
bool compareSim(mapPair p1, mapPair p2){
return p1.second>p2.second;
}
bool compareID(mapPair p1, mapPair p2){
return p1.first<p2.first;
}
void UserUserSim::getTop(int &user_id){
vector<USim> list;
vector<USim>* p=&list;
_map.insert(pair<int,vector<USim>*>(user_id,p));
_set.insert(user_id);
UserList* ul=UserDictionary::getInstance().getUser(user_id);
map<int,float> user_score_map;
vector<MovieScore>::iterator it;
vector<UserScore>::iterator it_movie; // the iterator for the movielist
for (it=ul->begin();it<ul->end();it++){ // for each movie rating in the Vector
int movie_id=(*it).movie_id;
MovieList* ml=MovieDictionary::getInstance().getMovie(movie_id);
for(it_movie=ml->begin();it_movie<ml->end();it_movie++){
int user_id=(*it_movie).user_id;
if (user_score_map.find(user_id)==user_score_map.end()){
user_score_map.insert(pair<int,float>(user_id,0));
}else{
user_score_map[user_id]+=(*it).rating*(*it_movie).rating;// the vector's user rating x the rating of the movie,user
}
}
}
//vector< pair<int,float> > user_score_v;
map<int,float>::iterator it_map;
for (it_map=user_score_map.begin();it_map<user_score_map.end();it_map++){} //<=============where error happens
}
请注意,如果我包含最后一行,我将收到如下错误:
g++ src/main.cpp src/CorpusExp.cpp src/MovieList.cpp src/MovieDictionary.cpp src/UserDictionary.cpp src/UserList.cpp src/UserUserSim.cpp -o recommend
src/UserUserSim.cpp: In member function ‘void UserUserSim::getTop(int&)’:
src/UserUserSim.cpp:81: error: no match for ‘operator<’ in ‘it_map < user_score_map. std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = int, _Tp = float, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, float> >]()’
make: *** [recommend] Error 1
对 c++ 几乎不熟悉,我检查了一些参考资料,我认为迭代地图是可以的,否则,我怎么能将地图中的一些元素(如 top k)复制到向量中?