Reputation: 1175
#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
}
Notice that if I include the last line, I will get the error as follows:
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
Barely new to c++, I checked some reference, I thought it is OK to iterate the map, otherwise, how could I copy some of the elements (like top k) out of the map into a vector?
Upvotes: 0
Views: 267
Reputation: 320481
Relational comparisons, like operators <
, >
and so on, are only available for random access iterators.
std::map
iterators are bidirectional iterators, which is a much weaker spec than random access. You cannot use relational comparisons with bidirectional iterators. Map iterators are only comparable for equality. Use !=
comparison in your loop condition instead of <
.
Moreover, it is always a good idea to use equality comparisons whenever you can and relational comparisons only when you really have to, i.e. rely on a weaker set of requirements whenever possible.
Upvotes: 5
Reputation: 6678
You need random-access iterators in order to compare them with "<", but std::map's iterators are not random-access (I'm not sure what they are, probably bidirectional).
Anyway, when iterating over a container, you should use "!=" for comparisson, not "<", whether with std::map iterators or with iterators of any other container.
Upvotes: 0
Reputation: 153840
You need to reduce the problem to a minimal amount of code! Most of the code seems to be entirely irrelavant to the problem!
The problem is that bidirectional iterators don't define a less-than relationship. You csn only compare them for equality or inequality. Just use
it != map.end()
instead of
it < map.end()
Upvotes: 1
Reputation: 689
In the last line, try this:
for (it_map = user_score_map.begin(); it_map != user_score_map.end(); ++it_map)
You should follow this pattern in all your code.
Upvotes: 2