[英]How do I insert into a set?
我知道明顯的方法是使用插入功能。 我想做的是將給定的電話號碼放入“電話”映射和phone_numbers集中。 可以將其放置在地圖中,但是當設置到phone_numbers時,它將出現段故障。 Add_Phone函數和Phone映射在名為Code_Processor的類內
class User {
public:
string username;
string realname;
int points;
set <string> phone_numbers;
};
map <string, User *> Phones;
int Code_Processor::Add_Phone(string username, string phone) {
//register the given phone # with given user
//put the phone on both Phones map and phone_numbers set
//use .insert()
User *nUser = new User;
map <string, User *>::iterator nit;
nit = Phones.find(username);
Phones.insert(make_pair(username, nUser)); //put into Phones map
nit->second->phone_numbers.insert(phone); //this is where is seg faults!!
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.