Thread: how to insert data in a map container

  1. #16
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hoi Elysia.

    I don't want to looking up stuff , just update it with a new value according to the data - file.

    Roelof

  2. #17
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use it so long as you know that the struct is inside the map. If it isn't, it will be added.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    230
    I know that.

    See this :
    Code:
    struct teamstats {
        string team_name;
        int played_games, point_made, points_against, woord;
    };
    
     map<string, teamstats> allteams;
    Roelof

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That declaration shows nothing.
    IF you have already added a teamstats to the map using the key "one", then yes, you can it.
    I am simply restating the condition. If you meet this, then go ahead.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22
    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.

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello Elysia,

    Is this better :
    Code:
    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;
         };
    Roelof

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by roelof View Post
    Hello Elysia,

    Is this better :
    Code:
    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;
         };
    Roelof
    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:
    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;
    };
    Horray for references!

    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:
    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);
    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:
    allteams[home_team].AddGame(home_score, away_score);
    That's right, it's now a one-liner! (excluding the default constructor and AddGame method)
    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"

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  12. #27
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  13. #28
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by roelof View Post
    This is very heavy stuff for a person who trying to learn c++ and write's his first programm.
    Yep, I imagine it is!
    Give it a go and feel free to ask more questions about it if you get stuck. (In this same thread is probably fine too)
    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"

  14. #29
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    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.
    Great point. So use a reference or an iterator.
    Code:
    //reference:
    teamstats &ref = allteams["name"];
    //iterator: 
    map<string, teamstats>::iterator it = allteams.find("name");
    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

  15. #30
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Im know trying to understand how I can make a constructor.

    I found this example
    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;
    }
    I see that classcomp for me is teamstats.
    and the map for me is map<string, teamstats> allteams;

    But what does fncomp do here.
    And why first , second and fourth ?

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding the Linker Map File
    By Hops in forum C Programming
    Replies: 0
    Last Post: 01-14-2010, 05:40 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM