map 容器
利用仿函数可以指定map容器的排序规则
对于自定义数据类型,map必须要指定排序规则,同set容器
map<int,int,compare> m;
m.insert(make_pair(1,2));
// An highlighted block
class MyCompare
{ public: bool operator()(int v1, int v2) {
return v1 > v2; }
};
void test01() {
//默认从小到大排序
//利用仿函数实现从大到小排序
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
m.insert(make_pair(4, 40));
m.insert(make_pair(5, 50));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key:" << it->first << " value:" << it->second << endl; }
}
int main() {
test01();
system("pause");
return 0;
}//
set容器排序
set容器默认排序规则为从小到大,掌握如何改变排序规则
```利用仿函数,可以改变排序规则
下面展示一些 `内联代码片`。
// A code block
var foo = ‘bar’;
#include <map>
class MyCompare {
public: bool operator()(int v1, int v2) {
return v1 > v2; }
};
void test01() {
//默认从小到大排序 //利用仿函数实现从大到小排序
map<int, int, MyCompare> m;
m.insert(make_pair(1, 10)); m.insert(make_pair(2, 20)); m.insert(make_pair(3, 30)); m.insert(make_pair(4, 40)); m.insert(make_pair(5, 50));
for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key:" << it->first << " value:" << it->second << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
```javascript
// An highlighted block
var foo = 'bar';