???Java????C++???????????
???????????? ???????[ 2013/4/17 9:53:25 ] ????????
?????????????????з????????????????????????????C++??????????????????????????÷??????????????????????????“С??”?????????????????????????????????????int??short??float????????????????????????????????????С???????????????C++????
using namespace std;
class MyTest
{
public:
MyTest(int value) {
_value = value;
}
~MyTest() {}
public:
bool operator< (const MyTest& o) const {
printf("operator < of MyTest is called here.
");
return _value - o._value < 0;
}
public:
int getValue() const {
return _value;
}
private:
int _value;
};
int main()
{
map<MyTest??int> m;
MyTest t5(5);
MyTest t2(2);
MyTest t10(10);
m.insert(make_pair(t5??5));
m.insert(make_pair(t2??2));
m.insert(make_pair(t10??10));
map<MyTest??int>::iterator it = m.begin();
printf("The count of the map is %d.
"??m.size());
for (; it != m.end(); ++it)
printf("key = %d?? value = %d
"??(*it).first.getValue()??(*it).second);
return 0;
}
//operator < of MyTest is called here.
???? //operator < of MyTest is called here.
???? //operator < of MyTest is called here.
???? //operator < of MyTest is called here.
???? //operator < of MyTest is called here.
???? //The count of the map is 3
???? //key = 2?? value = 2
???? //key = 5?? value = 5
???? //key = 10?? value = 10
????????????????????????MyTest??С??????????????????ε????????ú????????????????map??????????????С?????????????????????????????????map??????????ú??????????5?Σ??????????????????Java??compareTo????????????????÷????????????????????????С??????С???????????????????????????????????????????????????????????????????????????е????λ??????map??????insert????????????????????????????????????в??????????б????????????????????????α??????????true????????ü???????????????????????????λ?á?
????2?????????????????У?map<MyTest??int>???????????MyTest????????????????β???????????????????μ????????????????????????Ч???????????????????????????????????copy??????????????????less<Key>????????????????????MyTest??С????????????????????????????????????????????????????????less<Key>?????????????????????????????????????????????????????????????′????????
using namespace std;
class MyTest
{
public:
MyTest(int key) {
_key = key;
}
~MyTest() {}
public:
int _key;
};
struct MyTestComparator {
bool operator()(MyTest* first??MyTest* second) const {
printf("operator() of MyTestComparator is called here.
");
return first->_key - second->_key < 0;
}
};
using namespace std;
int main()
{
MyTest* mt0 = new MyTest(0);
MyTest* mt2 = new MyTest(2);
MyTest* mt1 = new MyTest(1);
map<MyTest*??int??MyTestComparator> mtMap;
mtMap.insert(make_pair(mt0??0));
mtMap.insert(make_pair(mt2??2));
mtMap.insert(make_pair(mt1??1));
printf("The count of map is %d.
"??mtMap.size());
map<MyTest*??int??MyTestComparator>::iterator it = mtMap.begin();
for (; it != mtMap.end(); ++it) {
printf("key = %d?? value = %d
"??(*it).first->_key??(*it).second);
delete (*it).first;
}
return 0;
}
//operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //operator() of MyTestComparator is called here.
???? //The count of the map is 3
???? //key = 0?? value = 0
???? //key = 1?? value = 1
???? //key = 2?? value = 2
???????????????????????????map??????less<Key>??????????????е??????????????????????????????????MyTest*???????????????Щ??????Java?е?Comparator<T>????
???????????????????Java??C++??????????????C++????????????????????????????????????????????Java?????·????????C++????????????????????????????????????????Ч??
??????
![](/images/ad-banner/ad-banner.png)
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11