Thread: how to insert data in a map container

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    how to insert data in a map container

    Hello,

    I have this.

    teamstats.team_name = msk
    teamstats.played_games = 1
    teamstats.points_made = 80
    teamstats.points_against = 20

    the map looks like this :
    Code:
    struct teamstats {
        string team_name;
        int played_games, point_made, points_against, woord;
    };
    
     map<string, teamstats> allteams;
    I know I have to use teamstat.insert but what arguments does this must have ?

    Roelof

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    The simplest way to insert items into a map involves using make_pair. In your case assuming that team_name is the key:
    Code:
    allteams.insert(make_pair(item.team_name, item));
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    struct teamstats {
    	int points_for,
    		points_against,
    		games;
    };
    
    
    int main() {
    	int i;
    	map<string,struct teamstats> eg;
    	struct teamstats tmp;
    	string names[3] = { "one", "two", "three" };
    
    /* add to map */
    	for (i=0;i<3;i++) {
    		tmp.points_for = i;
    		tmp.points_against = i*2;
    		tmp.games = 5;
    		eg.insert(pair<string,struct teamstats>(names[i],tmp));
    	}
    
    /* iterate thru map and verify */
    	map<string,struct teamstats>::iterator it = eg.begin();
    	while(it != eg.end()) {
    		cout << it->first << endl;
    		cout << "\tfor: " << it->second.points_for
    			<< " against: " << it->second.points_against
    			<< " (" << it->second.games << " games)\n";
    		it++;
    	}
    
    	return 0;
    }
    Notice I left "team name" out of the struct since that is the first element of the map. The output should be:

    one
    for: 0 against: 0 (5 games)
    three
    for: 2 against: 4 (5 games)
    two
    for: 1 against: 2 (5 games)


    Notice they are not in order.
    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

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

    I think I understand the trick.
    Can I do :
    Code:
    map<string, teamstats>::iterator it = allteams.find(one);
    played_games +=1 ;
    eg.insert(pair<string,struct teamstats>(names[i],tmp));
    If one has played one more game.

    Roelof

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    Can I do :
    Code:
    map<string, teamstats>::iterator it = allteams.find(one);
    played_games +=1 ;
    eg.insert(pair<string,struct teamstats>(names[i],tmp));
    If one has played one more game.
    Nope, again: "played_games" does not belong to the map. You had this misunderstanding before -- that somehow you could give a variable the same name as a struct member and use it as some kind of shorthand. No. If you have an independed "int played_games" this has nothing to do with the member of struct teamstats (that has the same name, but they have nothing else to do with one another).

    However, the "second" of the map pair is a struct with a member played_games, so:
    Code:
     
    map<string, teamstats>::iterator it = allteams.find("one");
    it->second.played_games++;
    will work. Notice the iterator uses indirect notation (it->second) whereas the members of the struct use direct notation (second.played_games) since it is a map of structs, not struct pointers.
    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

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    So if I want to change the value of points_made I have to do :

    it.second.points_made= it.second.points_made + 10 ;
    And don't I have to do something like insert to put the changesd value of points_made in the map ?

    Roelof

    Edit : I know there are not in order. Someone give me the tip to copy everything to a vector map and then sort it like I want it.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    it.second.points_made= it.second.points_made + 10 ;
    And don't I have to do something like insert to put the changesd value of points_made in the map ?
    it->second.points_made (also you can use "it->second.points_made += 10")

    No, you don't need to insert anything new. You are changing the value of an existing variable. The iterator is like a pointer to a member of the map, which can be identified by their "first" part, the string team name (find() etc use the first part, and it must be unique, ie, you cannot have two members with the same string identifier first part). The "second" part is the struct.
    Last edited by MK27; 06-09-2010 at 08:48 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

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

    I think I understand it.
    Thank you for the help and explanation.

    Roelof

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't understand why you're trying to complicate things so much, MK27.
    Instead of:
    Code:
    eg.insert(pair<string,struct teamstats>(item.team_name, item));
    It's better to use pianorain's suggestion, since you don't have to specify type:
    Code:
    allteams.insert(make_pair(item.team_name, item));
    Also, drop the "struct"! It's absolutely not necessary in C++. It only makes the code longer and harder to understand.
    Now, while make_pair is good and all, there's an even better way to do it that is even shorter and more understandable:
    Code:
    allteams[item.team_name] = item;
    Done. Simple, wasn't it?
    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.

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

    So if I understand it right.
    the only thing I have to do is allteams[teamstats.team_name] = teamstats to put everything in the map of structs ?

    Roelof

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To insert your struct into the map, yes.
    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.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    and the explanation of change a value is right from MK27 ?

    Roelof

  13. #13
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Using the [] operator for insert should only be done if you don't mind overwriting values. For example, let's say you've already got a team_name of "one" in your map. If you're expecting to insert a new team with the same name of "one", the [] operator will do this without any sort of warning. Using insert will protect you from accidently overwriting values already in your map. In the case above, using insert to insert your new team will return a pair of which the second value will tell you that you tried to insert a team with an existing team name.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    and the explanation of change a value is right from MK27 ?
    It works -- but there is a short sweet (should have been obvious...) option there too (mebbe I just forgot it):
    Code:
    allteams["one"].points_against += 10;
    Sorry roelof, I actually don't use C++ that much.


    Quote Originally Posted by Elysia View Post
    I don't understand why you're trying to complicate things so much, MK27.
    Instead of:
    It's better to use pianorain's suggestion, since you don't have to specify type:
    I see like zero (or very trivial) difference there, but alternatives are nice.

    Quote Originally Posted by Elysia View Post
    Now, while make_pair is good and all, there's an even better way to do it that is even shorter and more understandable:
    That is much better. C++ just got a little less clunky.
    Last edited by MK27; 06-09-2010 at 10:35 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. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MK27 View Post
    It works -- but it looks like there is a short sweet (should have been obvious...) option there too (mebbe I just forgot it):
    Code:
    allteams["one"].points_against += 10;
    Careful with that. Just remember that the index operator will insert the value if it doesn't exist and return a reference to the inserted item.
    If this is what you want, then this is fine. But don't go looking up stuff in the map with it.
    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.

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