Hello Guys,

I am new to C++ , I was trying to create a map key that contain multiple attribute and
i write my own compare function. But i am getting strange output with that may be my compare conditions are not right?
I write code as below

#include <iostream>
#include <map>
#include <stdio.h>
#include<stdint.h>
using namespace std;

struct PdpNetworkLocalKey
{
    PdpNetworkLocalKey(uint32_t a, bool b)
    {
      ip = a;
      isLocal = b;
    }
    PdpNetworkLocalKey()
    {
      ip = 0;
      isLocal = 0;
    }
    uint32_t ip;
    bool isLocal;

};

struct classcomp {
  bool operator() (const PdpNetworkLocalKey& lhs, const PdpNetworkLocalKey& rhs) const {
    //return ((lhs.ip <= rhs.ip) || ((lhs.ip == rhs.ip) && (rhs.isLocal != lhs.isLocal)));
    //return ((lhs.ip < rhs.ip));

    if(lhs.ip < rhs.ip) //my first true condition
    {
      printf("1:i am inside comp operatorr lhs %d  rhs %d\n", lhs.ip, rhs.ip);
      return true;
    }
    else if((lhs.ip == rhs.ip) && (rhs.isLocal != lhs.isLocal)) //my second true condition
    {
      printf("2:i am inside comp operatorr lhs %d bool %d  rhs %d bool %d\n", lhs.ip, lhs.isLocal, rhs.ip, rhs.isLocal);
      return true;
    }
    else
    {
      return false;
    }
  }
};


int main ()
{
    PdpNetworkLocalKey x1(10, false);
    PdpNetworkLocalKey x2(16, true);
    PdpNetworkLocalKey x3(15, true);
    PdpNetworkLocalKey x4(10, true);
    
    PdpNetworkLocalKey searchKey;
    searchKey.ip = 10;
    searchKey.isLocal = true;
 
    map <PdpNetworkLocalKey, uint32_t, classcomp > Ip_Key_Int_Map;
 
    Ip_Key_Int_Map.insert(map <PdpNetworkLocalKey, uint32_t, classcomp>:: value_type(x1 , 1000));
    Ip_Key_Int_Map.insert(map <PdpNetworkLocalKey, uint32_t, classcomp>:: value_type(x2 , 2000));
    Ip_Key_Int_Map.insert(map <PdpNetworkLocalKey, uint32_t, classcomp>:: value_type(x3 , 3000));
    Ip_Key_Int_Map.insert(map <PdpNetworkLocalKey, uint32_t, classcomp>:: value_type(x4 , 4000));
 
    printf("IP_KEY_SIZE %d\n", Ip_Key_Int_Map.size());
 
    map <PdpNetworkLocalKey, uint32_t, classcomp>::iterator it ;
    for(it = Ip_Key_Int_Map.begin(); it != Ip_Key_Int_Map.end(); it++)
    {
      printf("got %u  bool %d\n", (*it).first.ip, (*it).first.isLocal);

    }

    map <PdpNetworkLocalKey, uint32_t, classcomp>::iterator it1 ;
    it1 = Ip_Key_Int_Map.find(searchKey);
    if (it1 != Ip_Key_Int_Map.end())
    {
      printf("got %u  bool %d value %d\n", (*it1).first.ip, (*it1).first.isLocal, (*it1).second);
    }
    return 0;
}


OutPut:

1:i am inside comp operatorr lhs 10  rhs 16
1:i am inside comp operatorr lhs 15  rhs 16
1:i am inside comp operatorr lhs 10  rhs 15
1:i am inside comp operatorr lhs 15  rhs 16
1:i am inside comp operatorr lhs 10  rhs 15
2:i am inside comp operatorr lhs 10 bool 1  rhs 10 bool 0
2:i am inside comp operatorr lhs 10 bool 1  rhs 10 bool 0
IP_KEY_SIZE 4
got 10  bool 1
got 10  bool 0
got 15  bool 1
got 16  bool 1
2:i am inside comp operatorr lhs 10 bool 0  rhs 10 bool 1
1:i am inside comp operatorr lhs 10  rhs 15

What i am wondering .. why i am not able to get element with key (10, true) but map entry showing that it is present? Is it my compare function(definetly..i hope)? But what is the reason of not finding it? Could you guys give me any suggestion where am doing wrong?

Thanks

Looks like the troubles start from line #35 onwards.
You could shorten the comparison like so ..

bool operator() (const PdpNetworkLocalKey& lhs, const PdpNetworkLocalKey& rhs) const
{
  if(lhs.ip == rhs.ip)
    // Todo: Use lhs.isLocal and rhs.isLocal here to order the items as you like
    return ???;
  else
    // Order by ip
    return lhs.ip < rhs.ip;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.