插入地图

时间:2018-03-30 04:33:14

标签: c++ insert maps

我知道如何插入地图,但我遇到这个特定问题时遇到了麻烦。我觉得我这样做是正确的,但是当我去打印名字时,它只是没有打印出来。

New_User(string username, string realname, int starting_points):这会创建一个具有给定信息的新用户,并将其放入名称中。用户将从没有注册的电话号码开始。如果一切正常,你应该返回零。您应该从以下错误中返回-1而不创建任何新内容:

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

0 个答案:

没有答案