0

I want to map a struct(key) to an int(value). Then I want to find a value given a key (struct).

typedef struct {
  int  a, b; 
} TechId;  

struct our_cmp {
  bool operator() ( TechId a, TechId b ) const
  { return std::make_pair(a.a,a.b) > std::make_pair(b.a, b.b) ; }

};

int main() {
std::map<TechId, int, our_cmp> mymap;

TechId x;

if(mymap.find(x)) {
  printf("found");
}

}

There is a find function but I guess it is only for string/ints etc? Is there a possibility to adapt it to my struct?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Atoptix
  • 43
  • 3
  • 2
    Which C++ textbook are you using where you learned about `printf`, and to write code like`typedef struct { }`? If your goal is to learn current, modern C++ you should throw it away [and get a better C++ textbook](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), that doesn't send you in the wrong direction of writing prehistoric C instead of C++. – Sam Varshavchik Jul 06 '23 at 10:40
  • why do you guess? What makes you beleive it would only work for strings and integers? – 463035818_is_not_an_ai Jul 06 '23 at 11:00
  • nobody can remember all the standard library, you need a reference to look such things up. I recommend this one https://en.cppreference.com/w/cpp/container/map/find. It even includes an example of finding an element in a map of custom types – 463035818_is_not_an_ai Jul 06 '23 at 11:02
  • I learned a little bit C and now trying C++. I know its a bit messy, it is hard to just stay in one language – Atoptix Jul 06 '23 at 11:19
  • "it is hard to just stay in one language" why? Its harder to switch between two different languages when you arent familiar with either of them – 463035818_is_not_an_ai Jul 06 '23 at 11:27

1 Answers1

4

You need to check the iterator returned by find with the end() iterator of the map. If they are not equal, the element you searched for was found:

if (mymap.find(x) != mymap.end()) {
    printf("found");
}

Since C++20, you could also use the contains member function to do the same thing:

if (mymap.contains(x)) {
    printf("found");
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Because of my allocator (our_cmp) the compiler says: ‘class std::map’ has no member named ‘contains’... But without the comparing structure I cant map my struct. Maybe I have to write my own "find" function? – Atoptix Jul 06 '23 at 11:26
  • 1
    @Atoptix Allocator is not related, nor is comparator. `contains` is only available since C++20, you probably would have to enable it in your compiler. But the first version should work in any version of C++, it was always there. – Yksisarvinen Jul 06 '23 at 11:28
  • @Atoptix As mentioned in the answer, `contains` is only available since C++20. If you are using a version prior to C++20, then just use what we all used before C++20: `if(mymap.find(x) != mymap.end())` as is also mentioned in the answer. Why would you write your own `find` when there is already one that works? – Ted Lyngmo Jul 06 '23 at 11:31