Thread: problem defening a map of structs

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

    problem defening a map of structs

    hello,

    I try to define a map of structs.

    What I have is this :
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    using namespace std;
    
    int main() {
    
    struct teamstats {
    string team_name;
    int played_games, point_made, points_against;
    }
    
    map<string, struct teamstats> allteams;
    
    }
    I see now this message :

    C:\Users\wobben\Desktop\toernooi\test\main.cpp|14| error: template argument for 'template<class _T1, class _T2> struct std:air' uses local type 'main()::teamstats'|

    Can someone explain what went wrong here ?

    Roelof

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should define the struct outside of the main function, with a trailing semi-colon and proper indentation, e.g.,
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    
    using namespace std;
    
    struct teamstats {
        string team_name;
        int played_games, point_made, points_against;
    };
    
    int main() {
        map<string, teamstats> allteams;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    Solved,
    Now can i test if I can read the data from a file and put it into the struct.

    Roelof

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

    Last question I hope,

    Can I sort the map first on game_points and if there the same on played_games.

    Roelof

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A map is not meant to be sorted. It simply allows you to map one type to another.
    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. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    Then I have a problem.
    The outcome of the reading of the file and processing it , must be sorted so I can make a good ranking.

    back to the drawing board.

    Roelof

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use another container. For example, iterate your map, take every element, put it into a vector, sort it, then print your results.
    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.

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

    So read the map teamstats.
    Copy everything to a container of vectors and then sort it.

    Do I understand you right.
    And can a container of vectors be sorted on multiple arguments.
    Can I then not using a container of vectors in the first place ?

    Roelof

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A vector (or any other container that is sortable) can be sorted by multiple values. You decide exactly how to sort it by implementing your operator <.
    Could you use it from the beginning? I don't know. I haven't followed what exactly you do.
    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
    What I try to do is this.

    my struct looks like this :
    string team_name
    int played_games, game_points, made_points, against_points.

    Read a line from a text file.
    and put the content of that file to these variables : home_team, away_team, home_score and away_score.

    Then test if home_team is not already in the container.
    If so do this :
    increase played games for that team by 1
    look if that team has won , if so increase game_points by 3
    increase
    Increase made_points and against_points by the score of that game.

    The same story of the away_team.

    Read the following line and start over again with the home_team.

    If the whole file is read to the end.
    Sort everything and print it out.

    Roelof

    Edit :

    I see what's the problem is with vector containers.
    I don't know how many teams there are so I can't define the container well.
    Or make use of resize when finding a new team.
    Last edited by roelof; 06-05-2010 at 04:09 AM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    230
    Second problem
    I have this code:
    Code:
    #include <iostream>
    #include <map>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    struct teamstats {
        string team_name;
        int played_games, point_made, points_against;
    };
    
    int main() {
        map<string, teamstats> allteams;
        string str,myBuf;
    
        ifstream a_file ("test.txt");
        a_file >> str ;
        cout << str ;
     return 0 ;
    }
    Why gives cout no output.
    The file test.txt exist with one rule.

    Roelof

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Try adding a few lines to make sure, and read the whole file:
    Code:
    int main() {
        map<string, teamstats> allteams;
        string str,myBuf;
    
        ifstream a_file ("test.txt");
        if (!a_file.good()) cerr << "File not found!\n";
        while (a_file >> str) cout << str ;
    
        return 0 ;
    }
    Nb, this method of reading the file chomps newlines.
    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

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can use another container. For example, iterate your map, take every element, put it into a vector, sort it, then print your results.
    I actuall do this quite often. Many tests have revealed that iterating a map is far slower than iterating a vector although the code looks identical. Normally I build a vector from the items being used in the map and when the map changes I clear the vector and re-build it from the map. The easiest thing to do is to actually store map iterators in the vector so that when you iterate the vector you gain instant access to the map and it does not require an O(n log n) lookup per item in the vector. This works fine as long as you protect the map and make sure that if something is removed from the map the vector is updated to reflect this. If not you could have invalid iterators in the vector.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    store your strings (keys) in a vector, implement a sorting function based on the contents of the map value.

    then you can print them in your order, I dont see why its recommended to use another container?

  15. #15
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ...dont see why its recommended to use another container?
    Perhaps to support fast removal and insertion. Vectors are not good for insertion or removal since iterators beyond the point of removal are invalidated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with structs
    By darkstorm in forum C++ Programming
    Replies: 4
    Last Post: 06-03-2009, 04:18 PM
  2. Menu problem involving structs
    By NyHoK in forum C Programming
    Replies: 5
    Last Post: 03-31-2009, 10:00 AM
  3. Picture download problem
    By wingri in forum C Programming
    Replies: 10
    Last Post: 07-31-2007, 05:32 AM
  4. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  5. problem with a map iterator
    By anykey in forum C++ Programming
    Replies: 17
    Last Post: 04-29-2005, 11:49 PM