One example of STL map
代码
#include <map>
#include <iostream>
#include <string.h> //for memcmp
using namespace std;
typedef struct{
int connId;
int ueRef;
int rncId;
}UEREF_RNCID;
struct classcomp {
bool operator() (const UEREF_RNCID & l, const UEREF_RNCID & r) const
{
return memcmp(&l, &r, sizeof(UEREF_RNCID)) < 0;
}
};
UEREF_RNCID assignKey(int connId, int ueRef, int rncId)
{
UEREF_RNCID tempKey;
tempKey.connId = connId;
tempKey.ueRef = ueRef;
tempKey.rncId = rncId;
return tempKey;
}
int main(int argc, char **argv)
{
map<UEREF_RNCID, int, classcomp> m;
map<UEREF_RNCID, int, classcomp>::iterator it;
UEREF_RNCID key1, key2;
key1 = assignKey(-1,1,1);
key2 = assignKey(-1,2,1);
m[key1] = 0; //index is 0
m[key2] = 1; //index is 1
it = m.find(key1);
cout << it->second << endl; //will print 1
return 0;
}
#include <iostream>
#include <string.h> //for memcmp
using namespace std;
typedef struct{
int connId;
int ueRef;
int rncId;
}UEREF_RNCID;
struct classcomp {
bool operator() (const UEREF_RNCID & l, const UEREF_RNCID & r) const
{
return memcmp(&l, &r, sizeof(UEREF_RNCID)) < 0;
}
};
UEREF_RNCID assignKey(int connId, int ueRef, int rncId)
{
UEREF_RNCID tempKey;
tempKey.connId = connId;
tempKey.ueRef = ueRef;
tempKey.rncId = rncId;
return tempKey;
}
int main(int argc, char **argv)
{
map<UEREF_RNCID, int, classcomp> m;
map<UEREF_RNCID, int, classcomp>::iterator it;
UEREF_RNCID key1, key2;
key1 = assignKey(-1,1,1);
key2 = assignKey(-1,2,1);
m[key1] = 0; //index is 0
m[key2] = 1; //index is 1
it = m.find(key1);
cout << it->second << endl; //will print 1
return 0;
}