問題: 我添加條目在while循環地圖,每次啓動時在同一點,我。即先前輸入的條目被解散/覆蓋。添加在while循環中的條目爲std ::地圖將覆蓋最後輸入對
代碼:
#include <iostream>
#include <map>
int main(){
char* key=new char;
char *name, *address;
name=new char[80];
address=new char[80];
std::map<char*, char*> addressBook;
while(true){
std::cout<<"Please select action: \n";
std::cout<<"e - exit\n";
std::cout<<"a - add new address\n";
std::cout<<"p - print address book\n";
std::cin>>key;
switch(key[0]){
case 'a':
std::cin>>name>>address;
std::cin.clear();
std::cin.ignore(500,'\n');
addressBook[name]=address;
std::cout<<addressBook.size()<<"\n";
break;
case 'p':
for(std::map<char*,char*>::iterator it = addressBook.begin();it!=addressBook.end(); ++it)
std::cout<<it->first<<" "<<it->second<<"\n";
break;
default:
return 0;
}
}
}
的設置: 代碼::使用MinGW/GCC 4.7.1
他想在地圖上添加一個新的鍵值對 – Jacobr365