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;
}