Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. std::set to store pointers

std::set to store pointers

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    ginjikun
    wrote on last edited by
    #1

    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 :)

    CPalliniC 1 Reply Last reply
    0
    • G ginjikun

      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 :)

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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 a map, and so on. Please read the MSDN STL 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.

      In testa che avete, signor di Ceprano?

      G 1 Reply Last reply
      0
      • CPalliniC CPallini

        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 a map, and so on. Please read the MSDN STL 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.

        G Offline
        G Offline
        ginjikun
        wrote on last edited by
        #3

        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 :)

        A T CPalliniC 3 Replies Last reply
        0
        • G ginjikun

          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 :)

          A Offline
          A Offline
          Arman S
          wrote on last edited by
          #4

          Show us how you wrote your friend operator<

          -- ===== Arman

          G 1 Reply Last reply
          0
          • G ginjikun

            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 :)

            T Offline
            T Offline
            Tim Paaschen
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • G ginjikun

              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 :)

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              You have to pass your own comparer function object, otherwise the set 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.

              In testa che avete, signor di Ceprano?

              G 1 Reply Last reply
              0
              • A Arman S

                Show us how you wrote your friend operator<

                -- ===== Arman

                G Offline
                G Offline
                ginjikun
                wrote on last edited by
                #7

                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 :)

                A 1 Reply Last reply
                0
                • G ginjikun

                  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 :)

                  A Offline
                  A Offline
                  Arman S
                  wrote on last edited by
                  #8

                  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

                  G 1 Reply Last reply
                  0
                  • A Arman S

                    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

                    G Offline
                    G Offline
                    ginjikun
                    wrote on last edited by
                    #9

                    Thanks for all the replies Arman! i will try it out. thanks again! newbie :)

                    1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      You have to pass your own comparer function object, otherwise the set 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.

                      G Offline
                      G Offline
                      ginjikun
                      wrote on last edited by
                      #10

                      Thanks Pallini and Tim! i will try to write some code using what u have posted. thanks! newbie :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups