1 结构体应用在sort自定义的cmp函数中
# 扑克牌
#include<bits/stdc++.h>
using namespace std;
struct PKP{
int color; //花色 1~4
int num; //牌的数值 1~13,无大小王
int count; //相同数值牌的数量
};
int num_time[100];
bool cmp(PKP a, PKP b){
if(a.count != b.count)
return a.count > b.count;
if(a.num != b.num)
return a.num < b.num;
if(a.color != b.color)
return a.color < b.color;
}
int main(){
int N;
PKP pkp[100];
cin>>N;
for(int i = 0; i < N; i++){
cin>>pkp[i].color>>pkp[i].num;
num_time[pkp[i].num]++;
}
for(int i = 0; i < N; i++){
pkp[i].count = num_time[pkp[i].num];
}
sort(pkp,pkp+N,cmp);
for(int i = 0; i < N; i++){
cout<<pkp[i].color<<" "<<pkp[i].num<<endl;
}
system("pause");
return 0;
}
2 输入带有空格和逗号的字符串,并以逗号分割
输入:zhang san,li si,xiao kai,kui tian
string str;
getline(cin, str); //读取整行字符串
stringstream ss(str);//存入流
vector<string> strs; //字符串数组
char del = ',';// 分割符号为,
string tmp; //
while (getline(ss, tmp, del)) {
strs.push_back(tmp);
}
3 unordered_map<char , pair<int, int >> map;
赋值方式:
map['V'] = makepair(-1, 1);
取值方式:
map['V'].first; //取出-1
map['V'].second; //取出1