[multi_index] Sorting object based on some specified criteria !!

Hi all,
I have following kind of class and having some identifier and group id
associated with
particular procedure. I want to know if I have to get the procedures
relating particular
group for ex. (groupId == 1). How do I do using multiindex container?
Can somebody
please give me some hint or code if they have done something similar to
what I want
to do?
Thanks a lot,
Priyank
// class procedure test
class Procedure {
public:
Procedure(int _id, int _group ) : id_(_id), group_(_group) {
}
int get_id() const {
return id_;
}
int get_group() const {
return group_;
}
friend ostream& operator << (ostream& _os, const Procedure& _proc);
private:
int id_;
int group_;
};
ostream& operator << (ostream& _os, const Procedure& _proc)
{
_os << "PROCEDURE : " << _proc.id_ << endl;
return _os;
}
// Defines multiindex container that is hashed based on id
struct id {
};
typedef multi_index_container<
Procedure*,
indexed_by<
hashed_unique<
tag<id>, const_mem_fun
Procedure_Hashed_Pool;
typedef Procedure_Hashed_Pool::index<id>::type Procedure_By_Id;

Patel Priyank-PPATEL1
Hi all,
I have following kind of class and having some identifier and group id associated with particular procedure. I want to know if I have to get the procedures relating particular
group for ex. (groupId == 1). How do I do using multiindex container? Can somebody please give me some hint or code if they have done something similar to what I want to do?
Hello Priyank, I'm not sure I've understood what you're after,
but let me try anyway:
As you'll search for group, you'll need an additional index
(the one you've set up is based on process ID). The following
has not been compiled, beware typos:
struct id {};
struct group {};
typedef
multi_index_container<
Procedure*,
indexed_by<
hashed_unique<
tag<id>,
const_mem_fun
Procedure_Hashed_Pool; typedef Procedure_Hashed_Pool:: index<id>::type Procedure_By_Id; typedef Procedure_Hashed_Pool:: index<group>::type Procedure_By_Group; (I've decided to make the new index hashed non-unique, you could also have it ordered non-unique if you need the elements to be sorted by group ID --hashing only guarantees fast retrieval.) And now you can simply use equal_range() on this index to do what you want: int gr=...; const Procedure_By_Group& procedure_bg= procedure_pool.get<group>(); std::pair< Procedure_By_Group::iterator, Procedure_By_Group::iterator
p=procedure_bg.equal_range(gr);
p will hold the range of elements with group ID == gr. Is this what you were after? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
Joaquin M Lopez Munoz
-
Patel Priyank-PPATEL1