Inserting into a map

送分小仙女□ 提交于 2019-12-11 18:09:20

问题


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;
}

来源:https://stackoverflow.com/questions/49568410/inserting-into-a-map

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!