Hoi Elysia.
I don't want to looking up stuff , just update it with a new value according to the data - file.
Roelof
Hoi Elysia.
I don't want to looking up stuff , just update it with a new value according to the data - file.
Roelof
Can I use this allteams["one"].points_against += 10;
Points_against exist because I made it when I insert a record of a team the first time a team is being found in the text file.
Roelof
I know that.
See this :
RoelofCode:struct teamstats { string team_name; int played_games, point_made, points_against, woord; }; map<string, teamstats> allteams;
I came across this thread in my search to understand the map container and how to use it. I'm barely getting by with C (I just started programming a month ago), and C++ is an entirely different ball of wax. My application is much different, but the code in this thread has helped me immensely. Thank you so much.
Hello Elysia,
Is this better :
RoelofCode:map<string, teamstats>::iterator it = allteams.find(home_team); if (it == allteams.end()) { teamstats.home_team = home_team ; teamstats.played_games = 1 ; if home_score > away_score then teamstats.game_points = 3 ; teamstats.points_made = home_score ; teamstats.points_against = away_score; allteams[teamstats.team_name] = teamstats ; } else { allteams[home_team].played_games += 1; if home_score > away_score then allteams[home_team].game_points+=3; allteams[home_team].points_made += home_score ; allteams[home_team].points_against += away_score; };
That's very inefficient. You're looking up the same item 5 times. Here's a much clearer and more efficient way to do it:
Horray for references!Code:map<string, teamstats>::iterator it = allteams.find(home_team); if (it == allteams.end()) { teamstats &team = allteams[home_team]; team.home_team = home_team; team.played_games = 1; if (home_score > away_score) team.game_points = 3; team.points_made = home_score; team.points_against = away_score; } else { teamstats &team = it->second; team.played_games += 1; if (home_score > away_score) team.game_points +=3; team.points_made += home_score; team.points_against += away_score; };
However, if this were my own code I would consider that it is not necessary to store the team name both in the map's key and inside the struct that is the map's mapped value. I would also have a constructor and useful function called AddGame which all allows me to do the whole thing like this:
Wait, scratch that, what was I thinking! I would actually just write and use the default constructor, and replace all of that with just this:Code:map<string, teamstats>::iterator it = allteams.find(home_team); if (it == allteams.end()) allteams[home_team] = teamstats(home_score, away_score); else it->second.AddGame(home_score, away_score);That's right, it's now a one-liner! (excluding the default constructor and AddGame method)Code:allteams[home_team].AddGame(home_score, away_score);
Working out the constructor and contents of the AddGame member function I leave as an exercize for the reader. They're actually pretty simple though.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Oke,
I see your point.
But the meaning was if a team don't exist in the map then insert it into the map.
If a team does exist (else part) then update all the data.
If I understand your code right, then it seems that on else it will be inserted.
Roelof
The point iMalc was trying to get across is that index operator performs a lookup. Lookups take time which isn't constant. You're wasting time, so to speak.
Instead, if you were to remember where the struct is inserted, you can update it without a lookup, saving time instead.
Even in the single line code sample I posted if a team doesn't exist in the map then it insert its into the map, otherwise it updates it.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
oke
Then I will study it and read a lot about references before I can write the addgame and constructor part.
This is very heavy stuff for a person who trying to learn c++ and write's his first programm.
Roelof
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Last edited by MK27; 06-10-2010 at 05:27 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Oke,
Im know trying to understand how I can make a constructor.
I found this example
I see that classcomp for me is teamstats.Code:// constructing maps #include <iostream> #include <map> using namespace std; bool fncomp (char lhs, char rhs) {return lhs<rhs;} struct classcomp { bool operator() (const char& lhs, const char& rhs) const {return lhs<rhs;} }; int main () { map<char,int> first; first['a']=10; first['b']=30; first['c']=50; first['d']=70; map<char,int> second (first.begin(),first.end()); map<char,int> third (second); map<char,int,classcomp> fourth; // class as Compare bool(*fn_pt)(char,char) = fncomp; map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare return 0; }
and the map for me is map<string, teamstats> allteams;
But what does fncomp do here.
And why first , second and fourth ?
Roelof