Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Efficient container for large amounts of searchable data?

I am implementing a text-based version of Scrabble for a College project.

My dictionary is quite large, weighing in at around 400.000 words (std::string).

Searching for a valid word will suck, big time, in terms of efficiency if I go for a vector<string> ( O(n) ). Are there any good alternatives? Keep in mind, I'm enrolled in freshman year. Nothing TOO complex!

Thanks for your time!

Francisco

like image 590
F. P. Avatar asked Apr 28 '10 14:04

F. P.


People also ask

What is the most efficient container to access an element using an index?

The std::deque container is most efficient when appending items to the front or back of a queue. Unlike std::vector , std::deque does not provide a mechanism to reserve a buffer.

Which one is faster stack or vector?

vector will outperform list for almost every use case.

Which container provides random access to any of the elements of container?

A Random Access Container is a Reversible Container whose iterator type is a Random Access Iterator. It provides amortized constant time access to arbitrary elements.


1 Answers

If you wanted to go for something that is in the standard library, you could use std::set with the word as the key. That would give you logarithmic search time.

Since your dictionary is presumably static (i.e. created once and not modified), you could also use a std::vector, sort it using std::sort, and then use std::binary_search on the sorted vector to find a word. This would also give logarithmic search time, and would possibly be more space efficient than a set.

If you want to implement your own data structure, a trie would be a good choice.

like image 165
James McNellis Avatar answered Nov 15 '22 16:11

James McNellis