1

I'm wondering if there is a more appropriate structure for my needs.

I need to have a dictionary or strings (words). All I need to know is if a given word is in the dictionary.

It seems like a waste of memory to make a map of string,string. Is there a better way?

Thanks

jmasterx
  • 50,457
  • 91
  • 295
  • 535

4 Answers4

5

Use a std::set<string>. You can use std::set::find to check whether a word exists or not.

Naveen
  • 71,879
  • 44
  • 171
  • 229
0

What you want is a std::set< std::string >.

K-ballo
  • 78,684
  • 20
  • 152
  • 166
0

Naveen and K-ballo's answers are right. Here's something that might help you while choosing the right STL container for your needs. Note: This does not take into account C++11 types, but it will help you get started with the STL: enter image description here

Carl
  • 41,374
  • 10
  • 77
  • 101
  • Shameless plug: the question was asked to redraw this diagram for C++11. I proposed a text alternative: http://stackoverflow.com/questions/10699265/how-can-i-efficiently-select-a-standard-library-container-in-c11/10701102#10701102 – Matthieu M. Jun 05 '12 at 07:26
0

An alternative to std::set is std::unordered_set. The first is typically implemented as a red-black tree (logarithmic complexity) and the second is a hash table (average constant-time complexity). unordered_set is available in C++11, C++03 TR1, and Boost.

Blastfurnace
  • 17,981
  • 46
  • 54
  • 68