I’ve looked around a bit and have found no definitive answer on how to read a specific line of text from a file in C++. I have a text file with over 100,000 English words, each on its own line. I can’t use arrays because they obviously won’t hold that much data, and vectors take too long to store every word. How can I achieve this?
P.S. I found no duplicates of this question regarding C++
while (getline(words_file, word))
{
my_vect.push_back(word);
}
EDIT:
A commenter below has helped me to realize that the only reason loading a file to a vector was taking so long was because I was debugging. Plainly running the .exe loads the file nearly instantaneously. Thanks for everyones help.
If your words have no white-space (I assume they don’t), you can use a more tricky non-getline solution using a
deque!The above reads the entire file into a string and then tokenizes the string based on the
std::streamdefault (any whitespace – this is a big assumption on my part) which makes it slightly faster. This gets done in about 2-3 seconds with 100,000 words. I’m also using adeque, which is the best data structure (imo) for this particular scenario. When I use vectors, it takes around 20 seconds (not even close to your minute mark — you must be doing something else that increases complexity).To access the word at line 1:
Hope this has been useful.