64,282
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <map>
using namespace std;
class c2 {
public:
c2(int i, int j);
int m_id;
int m_id2;
};
c2::c2(int i, int j) {
m_id = i;
m_id2 = j;
}
struct ltc2
{
bool operator()(const c2& p1, const c2& p2) const
{
//return strcmp(s1, s2) < 0;
return p1.m_id < p2.m_id;
}
};
int main() {
map<c2,int,ltc2> m2;
c2 c_21(1, 2);
c2 c_22(2, 2);
c2 c_23(3, 2);
m2[c_21] = 11;
m2[c_22] = 22;
m2[c_23] = 33;
c2 to_find2(1, 10);
map<c2,int,ltc2>::iterator iter3;
iter3 = m2.find(to_find2);
if (iter3==m2.end()) {
cout << "--not found" << endl;
} else {
cout << "--found: " << iter3->first.m_id << ","
<< iter3->second << endl;
}
return 0;
}
struct ltc2
{
bool operator()(const c2& p1, const c2& p2) const
{
if( p1.m_id < p2.m_id) return true;
else if( p1.m_id > p2.m_id) return false;
return p1.m_id2 < p2.m_id2;
}
};
struct ltc2
{
bool operator()(const c2& p1, const c2& p2) const
{
return (p1.m_id < p2.m_id) || ( (p1.m_id == p2.m_id ) && p1.m_id2 < p2.m_id2 );
}
};