Alright as a preface I have a need to cache a relatively small subset of rarely modified data to avoid querying the database as frequently for performance reasons. This data is heavily used in a read-only sense as it is referenced often by a much larger set of data in other tables.
I’ve written a class which will have the ability to store basically the entirety of the two tables in question in memory while listening for commit changes in conjunction with a thread safe callback mechanism for updating the cached objects.
My current implementation has two std::vectors one for the elements of each table. The class provides both access to the entirety of each vector as well as convenience methods for searching for a specific element of table data via std::find, std::find_if, etc.
Does anyone know if using std::list, std::set, or std::map over std::vector for searching would be preferable? Most of the time that is what will be requested of these containers after populating once from the database when a new connection is made.
I’m also open to using C++0x features supported by VS2010 or Boost.
For searching a particular value, with
std::setandstd::mapit takes O(log N) time, while with the other two it takes O(N) time; So,std::setorstd::mapare probably better. Since you have access to C++0x, you could also usestd::unordered_setorstd::unordered_mapwhich take constant time on average.For
find_if, there’s little difference between them, because it takes an arbitrary predicate and containers cannot optimize arbitrarily, of course.However if you will be calling
find_iffrequently with a certain predicate, you can optimize yourself: use astd::maporstd::setwith a custom comparator or special keys and usefindinstead.