#include <iostream>
#include <map>
class compare
{
public:
bool operator()(const int c1, const int c2) const
{
std::cout << "In Compare: "
<< c1 << " -- " << c2 << std::endl;
return c1 < c2;
}
};
////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
// Create the map object.
std::map<int, char, compare> charMap;
// Populate the map with values.
std::cout << "Adding elements to the map:" << std::endl;
charMap.insert(std::map<int, char>::value_type(1,'A'));
charMap.insert(std::map<int, char>::value_type(3,'C'));
charMap.insert(std::map<int, char>::value_type(2,'B'));
charMap.insert(std::map<int, char>::value_type(4,'D'));
// Display the contents of the map.
std::cout << std::endl << "Contents of map: " << std::endl;
map<int, char>::iterator iter;
for (iter = charMap.begin();
iter != charMap.end(); iter++)
{
std::cout << (*iter).first << " --> ";
std::cout << (*iter).second << std::endl;
}
return 0;
}
这个程序中compare类是一个表示谓词的类
我不明白他是如何来控制元素的排列顺序问题
当中的参数c1,c2是映射中的哪两个值
多谢大家帮忙讲解一下...谢谢...