std::set to store pointers
-
Hi to all! i would like some help in using stl. how can i use it to store pointers. typedef std::set DataCol declaration does not seem to work. hope someone can provide me with a sample program. thanks for the help! newbie :)
There are many container classes in the STL library each able to store pointers. You have to choose the one that better fits your needs, for instance, if you need a dynamic array, then use the
vector
class. On the other hand, if you prefer to access elements by a key, then use amap
, and so on. Please read the MSDNSTL
documentation: there are specific samples. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
There are many container classes in the STL library each able to store pointers. You have to choose the one that better fits your needs, for instance, if you need a dynamic array, then use the
vector
class. On the other hand, if you prefer to access elements by a key, then use amap
, and so on. Please read the MSDNSTL
documentation: there are specific samples. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Hi! thanks for the reply. i have been browsing thru msdn unfortunately i cannot find any sample regarding storing pointers using stl set. i have written some code... i have a class which have a friend operator< function and the stl::set i have contains pointers to this class... when i run the code the items in the set are not sorted.. it seems like it does not pass the operator< that i have written... thanks again. hope to someone can help me out. thanks! newbie :)
-
Hi! thanks for the reply. i have been browsing thru msdn unfortunately i cannot find any sample regarding storing pointers using stl set. i have written some code... i have a class which have a friend operator< function and the stl::set i have contains pointers to this class... when i run the code the items in the set are not sorted.. it seems like it does not pass the operator< that i have written... thanks again. hope to someone can help me out. thanks! newbie :)
-
Hi! thanks for the reply. i have been browsing thru msdn unfortunately i cannot find any sample regarding storing pointers using stl set. i have written some code... i have a class which have a friend operator< function and the stl::set i have contains pointers to this class... when i run the code the items in the set are not sorted.. it seems like it does not pass the operator< that i have written... thanks again. hope to someone can help me out. thanks! newbie :)
If you store pointers in a set, the set is ordered by the pointer values (the addresses) and not by the values of your objects. You can either provide a customized comparer function object as the second template parameter or use the boost::ptr_set.
Regards, Tim
-
Hi! thanks for the reply. i have been browsing thru msdn unfortunately i cannot find any sample regarding storing pointers using stl set. i have written some code... i have a class which have a friend operator< function and the stl::set i have contains pointers to this class... when i run the code the items in the set are not sorted.. it seems like it does not pass the operator< that i have written... thanks again. hope to someone can help me out. thanks! newbie :)
You have to pass your own comparer
function object
, otherwise theset
will use the standard pointer comparison one (based on their addresses). I made a little test. Code is not elegant but it shows the point// test class, object ordering is based on _c member.
class Atest
{
friend struct comp;
public:
Atest(int i, char c)
{
_i = i;
_c = c;
}
bool comp( Atest * pb)
{
return ( _c < pb->_c) ? true : false;
}
void dump(FILE * fp)
{
fprintf(fp, "{%d,%c}\n", _i, _c);
}private:
int _i;
char _c;
};// The comparison function object
struct comp : public binary_function
{
bool operator()(const Atest * pa, const Atest * pb) const
{
return (pa->_c < pb->_c) ? true: false;
}
};// test program
int main(int argc, char* argv[])
{
Atest at1(5,'v'), at2(6,'a');
// note the comparison font object passed to set ctor
std::set< Atest *, comp> s;
s.insert(&at1);
s.insert(&at2);
// let's verify ordering
std::set< Atest *, comp>::iterator it;
for (it = s.begin(); it != s.end(); it++)
{
(*it)->dump(stdout);
}
return 0;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi Arman im not also sure if what i did was correct... i am working around the code that was also given as an answer to my previous post... the original declaration of the set was typedef std::set < CData > DataCollecton_t; and i changed it to typedef std::set < CData* > DataCollecton_t; the friend function is friend bool operator<(const CData &l, const CData &r) { if (l.priority_ DataCollecton_t; thanks! newbie :)
-
Hi Arman im not also sure if what i did was correct... i am working around the code that was also given as an answer to my previous post... the original declaration of the set was typedef std::set < CData > DataCollecton_t; and i changed it to typedef std::set < CData* > DataCollecton_t; the friend function is friend bool operator<(const CData &l, const CData &r) { if (l.priority_ DataCollecton_t; thanks! newbie :)
friend bool operator<(const CData &l, const CData &r) This is wrong. Now you have no CData objects inside the set but pointers, or more precise - integer values [addresses]. So the usual address [integer] comparision takes place. You can pass a comparator to the set ctor. A comparator is an object that acts as a function; struct cmp { bool operator () (const CData *p, const CData *q) { if (p->priority_) return true; return p->priority_ == q.priority_ && p->group_; } }; typedef std::set < CData*, cmp > DataCollecton_t;
-- ===== Arman
-
friend bool operator<(const CData &l, const CData &r) This is wrong. Now you have no CData objects inside the set but pointers, or more precise - integer values [addresses]. So the usual address [integer] comparision takes place. You can pass a comparator to the set ctor. A comparator is an object that acts as a function; struct cmp { bool operator () (const CData *p, const CData *q) { if (p->priority_) return true; return p->priority_ == q.priority_ && p->group_; } }; typedef std::set < CData*, cmp > DataCollecton_t;
-- ===== Arman
-
You have to pass your own comparer
function object
, otherwise theset
will use the standard pointer comparison one (based on their addresses). I made a little test. Code is not elegant but it shows the point// test class, object ordering is based on _c member.
class Atest
{
friend struct comp;
public:
Atest(int i, char c)
{
_i = i;
_c = c;
}
bool comp( Atest * pb)
{
return ( _c < pb->_c) ? true : false;
}
void dump(FILE * fp)
{
fprintf(fp, "{%d,%c}\n", _i, _c);
}private:
int _i;
char _c;
};// The comparison function object
struct comp : public binary_function
{
bool operator()(const Atest * pa, const Atest * pb) const
{
return (pa->_c < pb->_c) ? true: false;
}
};// test program
int main(int argc, char* argv[])
{
Atest at1(5,'v'), at2(6,'a');
// note the comparison font object passed to set ctor
std::set< Atest *, comp> s;
s.insert(&at1);
s.insert(&at2);
// let's verify ordering
std::set< Atest *, comp>::iterator it;
for (it = s.begin(); it != s.end(); it++)
{
(*it)->dump(stdout);
}
return 0;
}:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.