943,163 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 483
  • C++ RSS
Jan 29th, 2010
0

how to track objects and put them on a vector ?

Expand Post »
hello can I keep track of objects in a list and being able to sort them by there z var ?

do I use pointers ?
how would I do this ?

C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. vector<sometype> _objects_;
  4.  
  5. create_object(object_type)
  6. {
  7. new object_type myobject;
  8. _objects_.push_back(myobject);
  9. return _objects_.at(list_end);
  10. }
  11.  
  12. remove_object(int pos)
  13. {
  14. delete *_objects_.at(pos);
  15. _objects_.erase(pos);
  16.  
  17. }
  18.  
  19. sort_object_list()
  20. {
  21. object_list.sort(object.z);
  22. }
Reputation Points: 10
Solved Threads: 0
Light Poster
wwsoft is offline Offline
29 posts
since Jan 2010
Jan 29th, 2010
1
Re: how to track objects and put them on a vector ?
You can supply a predicate to the sort template from <algorithm> that does the comparison:
C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2. #include <vector>
  3.  
  4. void sort_object_list ( std::vector<sometype>& object_list )
  5. {
  6. struct compare {
  7. bool operator() ( const sometype& a, const sometype& b )
  8. {
  9. return a.z < b.z;
  10. }
  11. };
  12.  
  13. std::sort ( object_list.begin(), object_list.end(), compare() );
  14. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 29th, 2010
0
Re: how to track objects and put them on a vector ?
so how can I make this accept all object types ?
C++ Syntax (Toggle Plain Text)
  1. create_object(object_type)
  2. {
  3. new object_type myobject;
  4. _objects_.push_back(myobject);
  5. return _objects_.at(_objects_.end);
  6. }
Last edited by wwsoft; Jan 29th, 2010 at 6:16 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
wwsoft is offline Offline
29 posts
since Jan 2010
Jan 29th, 2010
0
Re: how to track objects and put them on a vector ?
Click to Expand / Collapse  Quote originally posted by wwsoft ...
so how can I make this accept all object types ?
C++ Syntax (Toggle Plain Text)
  1. create_object(object_type)
  2. {
  3. new object_type myobject;
  4. _objects_.push_back(myobject);
  5. return _objects_.at(_objects_.end);
  6. }
As long as your vector will contain homogenous types (i.e. they are all the same). You could use a templated function:

C++ Syntax (Toggle Plain Text)
  1. template<class T>
  2. T& create_object( vector<T> objects ){
  3. T obj;
  4. objects.push_back( obj );
  5. return objects.back();
  6. }

Please note that there is no good method for creating a container class to hold heterogenous data in c++. If you were brave, you could create a vector of void pointers, but you would have no way of knowing the type each element.

If you all of the items you need to put in the container are user defined types, then you could have each class inherit from some arbitrary base class, and then you could create a vector of pointers to the base type object:
C++ Syntax (Toggle Plain Text)
  1. class Derived1 : public Base{....}
  2. class Derived2 : public Base{...}
  3. vector<Base*> v;
  4. Derived1 a;
  5. Derived2 b;
  6. v.push_back(&a);
  7. v.push_back(&b);
Reputation Points: 152
Solved Threads: 41
Posting Whiz in Training
dusktreader is offline Offline
255 posts
since Jan 2010
Jan 30th, 2010
0
Re: how to track objects and put them on a vector ?
Thanks for the help !
Reputation Points: 10
Solved Threads: 0
Light Poster
wwsoft is offline Offline
29 posts
since Jan 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help sorting array in function...
Next Thread in C++ Forum Timeline: scientific calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC