Inserting into a map

2019-08-30 05:17发布

问题:

I know how to insert into a map, but I am having trouble doing it for this specific problem. I feel like I am doing this right but it just doesn't print anything out when I go to print the Names.

New_User(string username, string realname, int starting_points): This creates a new user with the given information, and puts it into Names. The user will start with no registered phone numbers. You should return zero if all is ok. You should return -1 from the following errors without creating anything new:

class User {
    public:
    string username;
    string realname;
    int points;
    set <string> phone_numbers;
};

class CodeProcessor {
  public:
   int New_User(string username, string realname, int starting_points);
  protected:
    map <string, User *> Names;
}
//trying to insert into map
int Code_Processor::New_User(string username, string realname, int starting_points) {
    //creates a new user with given info and puts it into Names
    //start with no registered phone #
    if(starting_points < 0) return -1;
    if(Names.find(username) != Names.end()) return -1;

    User *newUser = new User;

    Names.insert(make_pair(username, newUser));
    newUser->username = username;
    newUser->realname = realname;
    newUser->points = starting_points;

    return 0;
}

map <string, User *>::iterator lit;
for(lit = Names.begin(); lit != Names.end(); lit++) {
    fout << "ADD_USER  " << lit->second->username;
    fout << setw(5) << lit->second->points;
    fout << " " << lit->second->realname << endl;
}
标签: c++ insert maps