Reputation: 101
So, I have a text file (data.txt), It's a story, so just sentence after sentence, and fairly long. What I'm trying to do is to take every individual word from the file and store it in a data structure of some type. As user input I'm going to get a word as input, and then I need to find the 10 closest words(in data.txt) to that input word, using a function that finds the Levenshtein distance between 2 strings(I figured that function out though). So I figured I'd use getline() using " " as the delimiter to store the words individually. But i don't know what I should store these words into so that I can access them easily. And there's also the fact that I don't know how many words are in the data.txt file.
I may have explained this badly sorry, I'll answer any questions you have though, to clarify.
Upvotes: 2
Views: 1026
Reputation: 20730
You need a data structure capable to "contain" the strings you read. The standard library offer a number of "container" classes like:
Give a check to http://en.cppreference.com/w/cpp to the containers library and find the one that better fit your needs. The proper answer changes depending not only on the fact you have to "store them" but also on what you have to do with them afterwards.
Upvotes: 1
Reputation: 258608
In C++ you can store the words in a vector of strings:
#include <vector>
#include <string>
//....
std::vector<std::string> wordsArray;
// read word
wordsArray.push_back(oneWord);
Upvotes: 2