I wonder if I can shorten the code by someway initializing the vector of lists with predefined lists already in the same initialization (sorry if my question is unclear, please look to the code below
"hola", "mundo", "feliz" must be initialized before adding them to the vector.
My question if it is possible to initialize the list at the same time you are initializing the vector.
The code proposed by ne555 does exactly that. Or what else do you need?
Anyway, instead of hard-coding everything, it would be more elegant and flexible to do something like this:
1 2 3 4 5
std::vector<std::list<std::string>> questions;
for (size_t i = 0; i < TOTAL_NUMBER_OF_QUESTIONS; ++i)
{
questions.push_back(init_question(i));
}
...and implement a function:
1 2 3 4
std::list<std::string> init_question(const size_t i)
{
/*your code to initialize (e.g. load from file or DB) the i-th question here*/
}
BTW: Do you really need std::vector<std::list<std::string>> and can't use std::vector<std::vector<std::string>> or even std::vector<std::array<std::string,N>> instead?
> My question if it is possible to initialize the list at the same time you are initializing the vector.
No, not at the same time (there won't be a race condition). They are initialised one after the other; the vector is constructed after its lists are constructed, each list is constructed after its strings are constructed.
They can all be initialised by with a single statement in source code. For example:
@kigar6451, I've read that list is better than vector if many non-random insertions/deletions are used. And for filling up them, yes, I am using the functions as per your example.
@JLBorges, yes, this is exactly what I was looking for. Thanks.
I've read that list is better than vector if many non-random insertions/deletions are used. And for filling up them, yes, I am using the functions as per your example.
std::list (linked list) can, in theory, be faster, if you frequently insert/delete elements in the middle of the list.
(but benchmarks show that this is not necessarily the case, due to bad cache locality of std::list)
std::deque is supposed to be faster, if you frequently add/remove elements at the beginning or end of the list.
For anything else, std::vector is probably the simplest and fastest "list" (sequence container) class.
It appears to me, you just initialize your "lists" with a certain size and don't change them much afterwards.
Thus I'd probably go with:
1 2 3 4 5 6
std::vector<std::string> list;
list.reserve(42); // <-- pre-allocate capacity, so that no re-allocations are needed in the following
for (size_t i = 0; i < 42; ++i)
{
list.push_back("test");
}