注册 登录
编程论坛 C++教室

[求助]关于谓词

ichigo 发布于 2007-07-03 13:59, 835 次点击

#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是映射中的哪两个值
多谢大家帮忙讲解一下...谢谢...

8 回复
#2
kisscjy2007-07-03 16:10
帮你顶一下~~我也不知道~~
#3
aipb20072007-07-03 16:19
这你要看map的成员函数是怎样插入新元素的,跟踪下insert函数,看看实现代码。
不过stl封装的很深,想看明白还是不容易哦。
#4
ichigo2007-07-03 21:02
以下是引用aipb2007在2007-7-3 16:19:41的发言:
这你要看map的成员函数是怎样插入新元素的,跟踪下insert函数,看看实现代码。
不过stl封装的很深,想看明白还是不容易哦。

...看了也看不懂...那不是等于没说...

#5
zkkpkk2007-07-03 21:53
这个暑假该好好学学模板了,听说外面软件公司几乎统一口径的要求掌握
#6
aipb20072007-07-03 22:24
回复:(ichigo)以下是引用aipb2007在2007-7-3 16:19...
库函数那么多,每个实现搞这么透彻好复杂,我是不会去想这么多,会用就行。

呵呵~讨论下也不错,再研究研究吧!

(*_*)
#7
yuyunliuhen2007-07-04 09:50

有MSDN就够了,不知道就查,用的多了自然就记住了

#8
ichigo2007-07-04 13:06
我想知道这个例子中的C1跟C2分别是charMap.insert(std::map<int, char>::value_type(1,'A'));
中的哪个INT值
#9
ichigo2007-07-04 23:25
顶上来~~~~~~~~~~!!!哪位大哥帮忙解决一下...
1