回调函数:
#include <iostream>
#include <algorithm>
#include <deque>
using namespace std;
bool compare(int a, int b)
{
return a > b;
}
int main()
{
deque<int> d;
for (int i = 0; i < 10; i++)
{
d.push_back(i);
}
sort(d.begin(), d.end(), compare);
for (int i = 0; i < d.size(); i++)
{
cout << d[i] << " ";
}
cout << endl;
system("pause");
return 0;
}
仿函数:
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
class compare
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
int main()
{
map<int, int, compare> p;
p.insert(pair<int, int>(2, 10));
p.insert(make_pair(3, 5));
p.insert(map<int, int>::value_type(1, 7));
p[4] = 2;
for (map<int, int, compare>::iterator it = p.begin(); it != p.end(); it++)
{
cout << "first = " << it->first << " second = " << (*it).second << endl;
}
cout << endl;
system("pause");
return 0;
}