c++ - Selection of container to insert the multiple db recored -


in current task , need read read approximately 10,000 records (student data) database , need store same in container. need select container store records in order generate report. report generation option vendor specific 1 vendor wants dump records without sorting , other vendor wants generate report based on sorted name field. have selected vector end don't require kind of insertion in middle , no searching effort required first option (report without sorting fields) second option can suggest me if vector suitable option same introducing sorting based on name field.

below useful pointers found in scott meyers book (effective stl) in item 23 . believe based on below information second option sorted vector needs used still want through light on before come conclusion , start coding.

item 23. consider replacing associative containers sorted vectors

"**the standard associative containers typically implemented balanced binary search trees. balanced binary search tree data structure optimized mixed combination of insertions, erasures, , lookups. is, it's designed applications insertions, lookups, maybe more inser-tions, perhaps erasures, few more lookups, more insertions or erasures, more lookups, etc. key characteristic of sequence of events insertions, erasures, , lookups mixed up. in general, there's no way predict next operation on tree be"

as scott meyers suggests std::vector container storing data , manipulate - exception - if data quite big - not number of elements element size matters.

you can reduce size of data manipulate not manipulating data - manipulating indices data.

assuming have vector read file:

struct element { .... }; std::vector<element> data; readdata(somefile); 

you can generate "natural order" of records creating such vector of indices (see iota on cppreference):

using indices = std::vector<std::size_t>; indices naturalorder(data.size()); std::iota(naturalorder.begin(), naturalorder.end(), 0); // filled 0,1,2,... 

to print records using indices - define such algorithm:

template <typename container, typename indices, typename operation> void for_erach(container&& container, const indices& indices, operation&& op) {     (auto i: indices)          op(container[i]); }   // print in natural order for_each(data, naturalorder, [] (auto const& e) {      std::cout << e << std::endl;  });   

to sort, sort indices:

indices sorterdbyxorder = naturalorder; auto lessx = [&data](auto i, auto j) { return data[i].x < data[j].x; }; std::sort(sorterdbyxorder.begin(), sorterdbyxorder.end(), lessx); // print in sorted x  order std::cout << "sorted x:" << std::endl; for_each(data, sorterdbyxorder , [] (auto const& e) {      std::cout << e << std::endl;  });   

to have indices e.g. element.y == 7 - this:

indices onlyy7order; auto yis7 = [&data](auto i) { return data[i].y == 7; }; std::copy_if(naturalorder.begin(), naturalorder.end(),           std::back_inserter(onlyy7order),  yis7); 

if have performance problem read entire data std::vector - can try std::deque - in cases faster. rest of code presented not change - because std::deque , std::vector have similar interfaces...


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -