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
  • 52,639
  • 96
  • 311
  • 557

4 Answers4

5

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

Naveen
  • 74,600
  • 47
  • 176
  • 233
0

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

K-ballo
  • 80,396
  • 20
  • 159
  • 169
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
  • 43,122
  • 10
  • 80
  • 104
  • 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
  • 18,411
  • 56
  • 55
  • 70