C++ 程序,用户将在其中输入姓名并决定要显示的性别 (M/F)
C++ program where user will input names and decide which gender (M/F) to show
使用c++我需要在c++中使用map制作一个程序,其中性别(男或女)是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;
}
您可以尝试“设置”而不是地图。
#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;
}
使用c++我需要在c++中使用map制作一个程序,其中性别(男或女)是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;
}
您可以尝试“设置”而不是地图。
#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;
}