473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

managing objects in std::list

Hi,

I have a collection of lists, something like this:

std::list<Obj*> lists[10];

Now I add an Obj to one of the lists:

Obj* gary;
lists[3].push_front(gary);
Then later on, the Obj wants to remove itself from whichever list it was put
in (it's only allowed to be in one list). What's the best way to do this
efficiently?

My best idea is: when adding an Obj to a list, the Obj has to store the
address of the list, and also an iterator to the current start of the list
(i.e. where it's just been added). Later it can use these to remove itself.

Anyone have anything smarter than this?

ta,
G.A.
Jul 23 '05 #1
1 3041

Glen Able wrote:
Hi,

I have a collection of lists, something like this:

std::list<Obj*> lists[10];

Now I add an Obj to one of the lists:

Obj* gary;
lists[3].push_front(gary);
Actually, you have an array of lists of Obj pointers, and you
try to add an unitialized pointer to that list. That's undefined
behavior, and anything thath happens is Your Fault (TM).

Then later on, the Obj wants to remove itself from whichever list it was put in (it's only allowed to be in one list). What's the best way to do this efficiently?
This sounds like a bad design. There are a number of problems with this
approach: Your list doesn't contain objects but only the pointers. List
assumes it holds objects by value, which will work for pointers but
I'm afraid your objects don't have proper value semantics. You have no
obvious way to enforce your containment constraint.

The best way would be to wrap your Objects and the lists in a class.
(private members). Outside the class, you can only get an Object&.
The wrapper class is the one removing Objects from its own lists, and
thus enforces the association between Objects and lists.
My best idea is: when adding an Obj to a list, the Obj has to store the address of the list, and also an iterator to the current start of the list (i.e. where it's just been added). Later it can use these to remove

itself.

Won't work, you can move elements between identical lists (splice). In
that case, an Object may end up in a different list.

HTH,
Michiel Salters

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.