c++ - How to search a map with multiple keys with one of its keys? -


i have class key containg 2 integers

class tkey {   public:     int ikey1;     int ikey2; };  class tdata : public tkey {   public:     int idata; };   typedef map<tkey, tdata, less <tkey> >    mapdata; typedef mapdata::iterator                 itdata;  tmapdata mapdata;  mapdata.push_back(...); mapdata.push_back(...); 

now find item ikey1 == 10 example!

i can't use

  tkey thekey;   thekey.ikey1 = 10;   thekey.ikey2 = void;   // <<<<<    itdata = mapdata.find(thekey); 

how can that?

you can perform custom search using std::find_if function.

struct check_ikey1 {   check_ikey1( int ikey1) : ikey1_(ikey1) {}   bool operator()( const std::pair<int, int>& v ) const    {      return v.first == ikey1_;    } private:   int ikey1_; };   itdata = std::find_if( tmapdata.begin(), tmapdata.end(), check_ikey1(10) ); 

disclaimer:

written in browser. not compiled! taken this answer.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -