L2-039 清点代码库

先上一段错误代码;

这个代码只能处理数字全部为非负数的情况,因为是用string存的信息,大小比较也是通过string类型的compare字符串比较函数。

但是有两个知識点还是可以记录一下

1.map中设置结构体为key

2.string类型的字符串比较函数compare;

1.map中设置结构体为key

需要在struct中写 operator;

例如有一个map<node,int> 要按node.u的非升序排,如果node.u相等的话按node.s的非降序排;

主要要记住这个bool operator < 是怎么写的

struct node{
    string s; 
    int u;
     bool operator <(const node& n2) const{
        if(u==n2.u){
        	if(s.compare(n2.s)<0) return true;
        	else return false;
		}
        return u>n2.u;
    }
};
2.string类型的字符串比较函数compare

比如有两个string str1,str2; 

str1>str2    str1.compare(str2)>0;

str1=str2    str1.compare(str1)=0;

str1<str2    str2.compare(str1)<0;

放一下错误code:

别看23333 我只是记录一下,不然感觉写了好久,什么都没留下...   也顺便记录上面两个知识点。

#include<iostream>
#include<cstring>
#include<map>
using namespace std;
struct node{
    string s; 
    int u;
     bool operator <(const node& n2) const{
        if(u==n2.u){
        	if(s.compare(n2.s)<0) return true;
        	else return false;
		}
        return u>n2.u;
    }
};
int n,m,ans;
map<string,int>p;
map<node,int>q;
int main(){
    cin>>n>>m;
    getchar(); //!!!!!! 如果要使用getline(cin,str) 要保证前面没有\n; 
    for(int i=1;i<=n;i++){
        string str;
        getline(cin,str);
        if(p[str]) p[str]++;
        else{
            p[str]=1; ans++;
        }
    }
    for(auto it=p.begin();it!=p.end();++it){
        node nod;
        nod.s=it->first;
        nod.u=it->second;
        q[nod]=1;
    }
    printf("%d\n",ans);
    for(auto it=q.begin();it!=q.end();++it)
        cout<<it->first.u<<" "<<it->first.s<<endl;
    return 0;
}
正解来了~~~~

记录一下知识点; 第一次用vector(一种shishi的nanshuo感)

1.vector<int>作map的key

2.关于vector用的方法

1.vector<int>作map的key
map<vector<int>,int>p;

if(p[v]) p[v]++;  //v 是一个vector也可以直接做下标
else p[v]=1;

2.遍历vector,向添加vector中添加元素,排序vector
vector<int> v;
v.push_back(num);

for(auto j:v) printf(" %d",j);

vector<node>ans
sort(ans.begin(),ans.end());

//vector ans 中的 vector v;
ans.v; 

正确code:
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
struct node{
    vector<int>v;
    int ct;
    bool operator <(const node&n1) const{
        if(ct==n1.ct) return v<n1.v;
        return ct>n1.ct;
    }
}; //!!!
map<vector<int>,int>p;
vector<node> ans;
int n,m; 
int main(){
    cin>>n>>m;
    while(n--){
        vector<int> v;
        for(int i=0;i<m;i++){
           int num; scanf("%d",&num);
            v.push_back(num);
        }
        if(p[v]) p[v]++;  //v 是一个vector也可以直接做下标
        else p[v]=1;
    }
    cout<<p.size()<<endl;
    for(auto it=p.begin();it!=p.end();it++){
        node nod;
        nod.ct=it->second; nod.v=it->first;
        ans.push_back(nod);
    }
    sort(ans.begin(),ans.end());
    for(auto i:ans){
        printf("%d",i.ct);
        for(auto j:i.v) printf(" %d",j);
        printf("\n");
    }
    return 0;
}

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值