H-005
H-005

Reputation: 505

Can you create std::map with only keys?

So, I want to use std::map to find if an element exists. So I basically only need the key, not any value.

Is there any way to do something like std::map<std::string, void> for example? Or is there a better alternative to std::map for this kind of stuff?

Worst case scenario a different container like std::vector<std::string> could be used, but I'm curious if you can do this with std::map (std::map<std::string, bool> could also be used, but the bools would be wasted memory).

Upvotes: 0

Views: 372

Answers (1)

eerorika
eerorika

Reputation: 238311

Or is there a better alternative to std::map for this kind of stuff?

Yes. The container that you describe is a set. There are std::set and std::unordered_set corresponding to their map counterparts in the standard library.

Upvotes: 5

Related Questions