STL——map

https://blog.csdn.net/sunshinewave/article/details/8067862/

讲解的很详细,自己稍做总结:

1:map是1对1的

2:自动排序

3:6种构造,map<int,int> Map;最常见

4:插入的方法

①:Map.insert(make_pair<1,"asd">);   (make_pair函数https://blog.csdn.net/yockie/article/details/6980692)  

       Map.insert(paid<int,string>(1,"asd"));

②:Map.insert(map<int,string>::value_type (1,"asd"));

③:直接map[1]="asd";

区别:map不会重复插入key值相同的元素①②方法插入相同元素会失败,③方法是会重复掉之前的数据。

5:STL里面的元素都是默认的按照<排列

6:map按照value值排序:

因为sort是只能对无序的容器排序,map不行,所以借助vector

struct CmpByValue {  
  bool operator()(const pair<string,int> & lhs, const pair<string,int> & rhs)
  {return lhs.second > rhs.second;}  
};
 
void substrcount(string str)
{
    map<string,int> substr;
    string subs;
    for(int i =1; i<str.size();i++)
        for(int j =0; j<=str.size()-i;j++)
        {
            subs = str.substr(j,i);
            if(substr.find(subs)==substr.end())
                substr[subs] = 1;
            else
                substr[subs] = substr[subs]+1;
        }
 
    vector<pair<string,int>> counts(substr.begin(),substr.end());
    sort(counts.begin(),counts.end(),CmpByValue());//如果是只想对vector的前几个数排序,那就用iterator
    cout<<counts.begin()->first<<endl;
    cout<<counts.begin()->second<<endl;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值