Sorting user defined structure in C++ -


i have data structure of following form:

struct e{  unsigned k;  unsigned c;  unsigned v[100]; };  main() {  vector<e*> vece;  e e1;  e1.k=10; e1.c=20; e1.v[0]=30; e1.v[1]=31;  vece.push_back(&e1);  e e2;  e2.k=101; e2.c=28; e2.v[0]=82; e2.v[1]=32;  vece.push_back(&e2);  //sort vece e.k of structure } 

i have large vece containing thousands of objects of type e. need sort vece again , again. fastest way can sort vece in-memory -- here vece sorted on e.k. 1 of ways 1 can same building priority queue. priority rather expensive due insertions. there other fast way can sort vece on "k".

also after sorting , outputting results user want delete vece entirely..but can not consists of pointers e. how can destroy vece completely, values pointed e deleted.

the gcc version using is: gcc (ubuntu/linaro 4.6.4-6ubuntu2) 4.6.4

given e quite big structure, should have vector<e*>, swapping elements fast swapping single integer. can use either sort or stable_sort standard header <algorithm> (see http://www.cplusplus.com/reference/algorithm/).

vector<e*> vece; sort(vece.begin(),vece.end(),compe); 

with:

bool compe(e* first,e* second) {     return (first->k < second->k); } 

at end delete elements way:

for(auto e: vece) {     delete e; } 

you don't need delete vector itself, because has automatic storage duration, , destroyed @ scope exit.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -