Hundreds of Strings, Ideas?

I'm writing a program that will hold hundreds of strings. Sadly, they are all unique, so I can't use any clever ways to generate them. I need advice on how to store these hundreds of strings. I do not want them to be edited by the user in any way. I just need them waiting to be called into the output display.

Is there anywhere to put hundreds of strings other than in the main function? I was just wondering if there was somehow a better way.
Not sure what you mean by "edited by the user"; the client of the code or the user of the program?
Anyway, depending on what you mean, here's my solutions:
Make them a const static container inside some function that you can access them from. static makes them live for the lifetime of the program.
1
2
3
4
5
const std::string& getString(std::size_t index)
{
    const static std::vector<std::string> strings = {"These are", "some very cool", "strings"};
    return strings[index];
}


Or instead of hardcoding them, make a function to load them into a container from a file that lists the strings.
file:
1
2
3
These are
some very cool
line-delimited strings!!!

load:
1
2
3
4
5
6
7
std::ifstream f("strings.txt");
std::vector<std::string> strings;
std::string line;
while(getline(f, line))
{
    strings.push_back(line);
}


Replace std::vector with an std::map functionality for log(n) access.

I would suggest loading from files, hardcoding almost always bites you back in the end.
Last edited on
Yea, I was worried about hardcoding into the main. I'll look more into reading the text from another file, thanks!
Topic archived. No new replies allowed.