2023.3.28 学习总结
1.map容器的排序
map的排序默认按照key从小到大进行排序,但有以下几点需要注意:
1 按照key从大到小进行排序。
2 key的第1个元素是结构体。
3 想按value(第二个元素)排序。
1.让map中的元素按照key从大到小排序
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main(){map<string, int, greater<string> > mapStudent; //关键是这句话mapStudent["LiMin"]=90;mapStudent["ZiLinMi"]=72;mapStudent["BoB"]=79;map<string, int>::iterator iter=mapStudent.begin();for(iter=mapStudent.begin();iter!=mapStudent.end();iter++){cout<<iter->first<<" "<<iter->second<<endl;}return 0;
}
重点就是这行
map<string, int, greater<string> > mapStudent;
key值是string类型的就添加greater<string>,是int类型就greater<int>,根据实际情况。
1.1重定义map内部的Compare函数,按照键字符串长度大小进行排序
#include <map>
#include <string>
#include <iostream>
using namespace std;
// 自己编写的Compare,实现按照字符串长度进行排序
struct cmp {bool operator()(const string& k1,const string& k2) const{return k1.length() < k2.length();}
};
int main() {map<string, int,cmp> mapStudent; //这里注意要换成自己定义的comparemapStudent["LiMin"] = 90;mapStudent["ZiLinMi"] = 72;mapStudent["BoB"] = 79;map<string, int>::iterator iter = mapStudent.begin();for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++) {cout << iter->first << " " << iter->second << endl;}return 0;
}
这个就是不用cmp也可以,但是我们不能保证在编程问题中现成的排序能满足要求,所以我们一定要会自定义cmp。
2.key是结构体的排序
因为结构体可以用cmp或者内嵌operator进行排序,但是map中只能用内嵌operator进行排序
#include <map>
#include <string>
#include <iostream>
using namespace std;
typedef struct stuInfo
{int id;string name;bool operator < (const stuInfo & r) const {//这个函数指定排序策略,按id排序,如果id相等的话,按name排序 if (id != r.id) {return id < r.id;}else {return name > r.name;}}
};//学生信息int main() {/*用学生信息映射分数*/map<stuInfo, int>stu;stuInfo s1;s1.id = 1;s1.name = "s1";stu[s1] = 90;s1.id = 2;s1.name = "s2";stu[s1] = 80;s1.id = 1;s1.name = "u1";stu[s1] = 80;for (auto it=stu.begin(); it != stu.end(); it++) {cout << it->first.id << " " << it->first.name << " " << it->second << endl;}return 0;
}
3.将map按value排序
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
typedef pair<string, int> PAIR;
bool cmp_by_value(const PAIR& lhs, const PAIR& rhs) { return lhs.second < rhs.second;
}
struct CmpByValue { bool operator()(const PAIR& lhs, const PAIR& rhs) { return lhs.second < rhs.second; }
};
int main(){ map<string, int> name_score_map; name_score_map["LiMin"] = 90; name_score_map["ZiLinMi"] = 79; name_score_map["BoB"] = 92; name_score_map.insert(make_pair("Bing",99)); name_score_map.insert(make_pair("Albert",86)); /*把map中元素转存到vector中*/ vector<PAIR> name_score_vec(name_score_map.begin(), name_score_map.end()); sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue()); /*sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);也是可以的*/for (int i = 0; i != name_score_vec.size(); ++i) { cout<<name_score_vec[i].first<<" "<<name_score_vec[i].second<<endl; } return 0;
}
2.筛法(判断素数)
筛法求素数的核心是依次筛掉2~sqrt(N)中素数的倍数,直到a中仅剩下素数为止,这种方法巧用数组,利用数组作为标志变量,是一种效率较高的求素数的算法。
#include <iostream>
#define MAX 50
using namespace std;int flag[MAX];int main() {//flag数组中的值为1表示不是素数,0现在表示的是还没有进行判断的数flag[0] = 1;flag[1] = 1;int j;for (int i = 2; i <= sqrt(MAX); i++) {//2是素数if (!flag[i]) {j = 2;while (i * j <= MAX) {flag[i * j] = 1;//i的倍数不是素数j++;}}}for (int i = 0; i <= MAX; i++) {//现在flag数组中值为0的下标表示的是素数if (!flag[i]) {cout << i << " ";}}return 0;
}