I have to fetch values from a database and store them in a vector in
my c++ application.
What is faster? if i fetch the values sorted from the database with a
'order by' clause in the sql-statement or if i fetch them into the
vector unsorted and sort the vector then?
Thanx for your help
Greetz
Jens
Instead of std::vector, why not use a std::map<Key, record>, where key
is the data to be used as a sorting criterion. If the compare
operation is already defined (like for a built-in data type), then you
can just use subscripting or the insert function to make the map,
which will automatically be sorted. If you need to define a user
specific compare operation, you can specify the appropriate functor as
a template parameter or ctor argument.
Also, I don't know anything about SQL's interaction with C++ in
particular, but if it is possible to get only pointers to the records
(IOW std::map<Key, record *>), that will avoid copying the records,
which will probably save time.
Oh .. I just re-read your post and saw you said "values from the
database" ... if you really just want an ordered list of comparable
values, you can use std::set (instead of std::map) as above ... this
might even be more efficient.
HTH, Dave Moore