help please!
-
hi! i would like to ask what is the best way to store and sort (e.g. priority queue,multimap,sorted vector, etc) my date which consist of 2 criterias. first is the priority, then according to group. sample is... priority 1, group1 data 1 data 2 priority 1, group 2 data 1 data 2 data 3 priority 2, group 4 data 1 group 5 data 1 ... each group may consist of 1 or more data i was thinking of priority_queue however some of the data are stored til the end of the application. i am not sure if queue have somekind of peek method. and vector using sort method, but the data are added throughout the lifetime of the application indefinitely and not just 1 time... sort may be too expensive to be called many times. thanks for any help! newbie :)
-
hi! i would like to ask what is the best way to store and sort (e.g. priority queue,multimap,sorted vector, etc) my date which consist of 2 criterias. first is the priority, then according to group. sample is... priority 1, group1 data 1 data 2 priority 1, group 2 data 1 data 2 data 3 priority 2, group 4 data 1 group 5 data 1 ... each group may consist of 1 or more data i was thinking of priority_queue however some of the data are stored til the end of the application. i am not sure if queue have somekind of peek method. and vector using sort method, but the data are added throughout the lifetime of the application indefinitely and not just 1 time... sort may be too expensive to be called many times. thanks for any help! newbie :)
-
Explain in deatail.... What is your actual requirment I thnink your question is incomplete
Regard's Kaushik
Hi Kaushik thanks for the response! the requirement is to store according to priority and group each of the data that are received.... receiving of data happens indefinitely (e.g. every 1 second/5seconds) each data as mentioned should be arranged according to their priority and group they belong. thus the list should look something like below priority 1 group 1 data 1 data 2 group 2 data 1 group 3 data 1 data 2 priority 2 group 4 data 1 group 5 data 1 data 2 ... the group number is unique, thus there can only be a single group 1 in the entire list... and so on... the question is how to efficiently implement this. keeping in mind that the data coming in may occur every second or maybe less... also the data are stored indefinitely depending on the priority.. e.g. priority 1 may be removed after processing but priority 2 after processing will stil remain in the list... sorry for the lenghty explanation... thanks again for any response! newbie :)
-
Hi Kaushik thanks for the response! the requirement is to store according to priority and group each of the data that are received.... receiving of data happens indefinitely (e.g. every 1 second/5seconds) each data as mentioned should be arranged according to their priority and group they belong. thus the list should look something like below priority 1 group 1 data 1 data 2 group 2 data 1 group 3 data 1 data 2 priority 2 group 4 data 1 group 5 data 1 data 2 ... the group number is unique, thus there can only be a single group 1 in the entire list... and so on... the question is how to efficiently implement this. keeping in mind that the data coming in may occur every second or maybe less... also the data are stored indefinitely depending on the priority.. e.g. priority 1 may be removed after processing but priority 2 after processing will stil remain in the list... sorry for the lenghty explanation... thanks again for any response! newbie :)
Perhaps this will give you some ideas: -------------------------------------- // Console.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include class CData { public: CData(int priority, int group) : priority_(priority) , group_(group) {} // Define the sorting criteria. friend bool operator<(const CData &l, const CData &r) { if (l.priority_ inline std::basic_ostream& operator<<(std::basic_ostream &os, const CData &d) { os << "Priority: " << d.priority_ << std::endl; os << "Group: " << d.group_ << std::endl; return os; } typedef std::set DataCollecton_t; int main(int arvc, char* argv[]) { // Populate the collection. DataCollecton_t coll; coll.insert(CData(3, 2)); coll.insert(CData(3, 3)); coll.insert(CData(2, 2)); coll.insert(CData(1, 1)); coll.insert(CData(2, 3)); coll.insert(CData(1, 2)); coll.insert(CData(2, 1)); coll.insert(CData(1, 3)); coll.insert(CData(3, 1)); // Print the results. std::copy(coll.begin(), coll.end(), std::ostream_iterator(std::cout)); return 0; } Steve
-
Perhaps this will give you some ideas: -------------------------------------- // Console.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include class CData { public: CData(int priority, int group) : priority_(priority) , group_(group) {} // Define the sorting criteria. friend bool operator<(const CData &l, const CData &r) { if (l.priority_ inline std::basic_ostream& operator<<(std::basic_ostream &os, const CData &d) { os << "Priority: " << d.priority_ << std::endl; os << "Group: " << d.group_ << std::endl; return os; } typedef std::set DataCollecton_t; int main(int arvc, char* argv[]) { // Populate the collection. DataCollecton_t coll; coll.insert(CData(3, 2)); coll.insert(CData(3, 3)); coll.insert(CData(2, 2)); coll.insert(CData(1, 1)); coll.insert(CData(2, 3)); coll.insert(CData(1, 2)); coll.insert(CData(2, 1)); coll.insert(CData(1, 3)); coll.insert(CData(3, 1)); // Print the results. std::copy(coll.begin(), coll.end(), std::ostream_iterator(std::cout)); return 0; } Steve