#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<string>
using namespace std;
struct node{
int val;
string str;
};
bool cmp( node&a, node&b){
return a.val==b.val? a.str<b.str: a.val>b.val;
}
vector<node> infor;
int main(){
int n,m,tempv,type;
string tempt,tstr;
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>tempt>>tempv;
infor.push_back({tempv,tempt});
}
for(int i=1;i<=m;i++){
cin>>type>>tstr;
printf("Case %d: %d %s\n",i,type,tstr.c_str());//不能通过‘...’传递有不能平凡复制的类型‘输出是会出问题的,因为“%s”要求后面的对象的首地址,但是string不是这样的一个类型。
vector<node> ans;//c_str()注意首地址
unordered_map<string,int> t3;
int cnt=0,sum=0;//条件重置
if(type==1){
for(int j=0;j<n;j++){
if(tstr[0]==infor[j].str[0]){//这写错了(注意)
ans.push_back(infor[j]);
}
}
}
else if(type==2){
for(int j=0;j<n;j++){
if(tstr==infor[j].str.substr(1,3)){//下标从1开始3个字符
cnt++;
sum+=infor[j].val;
}
}
if(cnt!=0)
printf("%d %d\n",cnt,sum);
}
else if(type==3){
for(int j=0;j<n;j++){
if(tstr==infor[j].str.substr(4,6)){
ans.push_back(infor[j]);
}
}
}
//type 2也有可能没有
if(cnt==0&&type==2){
printf("NA\n");
}
else if(type!=2&&ans.size()==0){
printf("NA\n");
}
else if(type==1){
sort(ans.begin(),ans.end(),cmp);
for(int i=0;i<ans.size();i++){
printf("%s %d\n",ans[i].str.c_str(),ans[i].val);
}
}
else if(type==3){
//sort(ans.begin(),ans.end(),cmp);
for(int i=0;i<ans.size();i++){
t3[ans[i].str.substr(1,3)]++;
}
ans.clear();
for(auto it=t3.begin();it!=t3.end();it++){
ans.push_back({it->second,it->first });
}
sort(ans.begin(),ans.end(),cmp);
for(int i=0;i<ans.size();i++){
printf("%s %d\n",ans[i].str.c_str(),ans[i].val);
}
}
}
return 0;
}
総括する
1.変数名が正しくコピーされていない各部分に必要な変数について明確に考える必要があります
2。「...」を渡して、通常の方法ではコピーできない型を渡すことができます。「%s」には次のオブジェクトの最初のアドレスが必要ですが、文字列はそうではないため、出力に問題があります。タイプ。、%sを出力するには、... c_str()を使用します
3.mapERROR数クライアント历for(auto it = t3.begin(); it!= t3.end(); it ++){ ans.push_back({it-> second、it-> first}); }
英語
質問誤って入力された変数の間違いを減らす方法は、より具体的かつ簡潔にする必要があります