I have a very large number of objects (about 30k). What is the best way to store and access them? They all have a specific Id, but i also would like to filter and search for them with their name, category etc. Its a quite simple class, something like the following:
class objclass {
int id;
std::string name;
...
}
I was thinking about SQL, but I don't know if thats the best way.
Thanks in Advance! :)
Update: Thx Guys! I think i'll go with a vector then. And thx for clarifying that 30k isnt that big^^ For me, who never handled with such amounts of Data it seemed quite large ;)
std::vector
sounds like a perfect fit. If you know in advance how many elements you get, use vector::reserve
or vector::resize
to not overallocate. Otherwise use vector::shrink_to_fit
after lots of insertions.
To speed up searches on the id
, sort the vector
on it and use a binary_search/lower_bound
.
If you have lots of strings with the same content, use a flyweight class. This can also substantially speed up string comparisons.
To search quickly on string members, get a vector
of iterators into your container and sort those or go for a boost::multi_index
.
A small calculation to back that up: assuming int
is 4 bytes, your strings average 20 letters, 30 000 elements, makes roughly 2 megabyte. Nothing to worry about.