使用 c 我需要在 c 中使用 map 制作一個程式,其中性別(M 或 F)是 6 個輸入名稱的鍵,然后它會要求用戶選擇要顯示的性別(男性或女性)。我被這個困住了,我不知道下一步該怎么做。
#include <iostream>
#include <set>
using namespace std;
int main(){
set<pair<string,string>> s;
set<pair<string,string>>::iterator itr;
string name;
string gender;
cout<<"Enter 6 names with genders : "<<endl;
for(int a=1; a<=6; a ){
cin>>gender;
getline(cin, name);
s.insert({name,gender});
}
cout<<endl;
cout<<"Enter the gender(M/F): ";
cin>>gender;
for (itr = s.begin(); itr != s.end(); itr ) {
if(itr->second==gender)
cout<<" "<<itr->first<<" "<<itr->second<<endl;
}
cout << endl;
return 0;
}
uj5u.com熱心網友回復:
您可以嘗試“設定”而不是地圖。
#include <iostream>
#include <set>
using namespace std;
int main(){
set<pair<char,string>> s;
cout<<"Enter 6 gender and name : "<<endl;
char gender; string name;
for(int a=1; a<=6; a ){
cin >> gender; getline(cin, name);
s.insert({gender,name});
}
set<pair<char,string>>::iterator itr;
for(itr = s.begin(); itr!=s.end(); itr ){
cout << itr->first << " " << itr->second << endl;
}cout << endl;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/425966.html
下一篇:臨時物件中的字串文字?
