 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Craig Guest
|
Posted: Fri Jul 16, 2004 1:58 pm Post subject: using STL deque and structures |
|
|
I have an application that uses a deque of deques to hold information
from an NDS tree. Each deque within the deque represents a layer within
the tree and the elements of the deque are structs containing object data.
I have a situation where I need to add more information to an existing
deque but seem unable to come up with a solution that works. It appears
that deque.push_back takes a copy of the current deque and puts it into
the other deque. If I assign a local deque the deque at some layer such
as localdq = Deque.at(level) modify it and try to put it back where it
came from the cleanup mechanisms clear out the data in the local deque
which is actually the one I expect to be saved in the storage deque.
I am stuck here as I don't understand the problem well enough. I
realise its an object life problem but can't come up with a workable
solution that doesn't involve re-writing the entire thing.
Just for clarity this is what I do:
within a thread
create local deque
if its an existing level we are getting extra data for
ldq = StorageDeque.at(level);
else do nothing
find data create new structs and fill them
push back the structs into a deque for that level
if its a new deque
push back the deque into the main storage deque
else erase the existing deque and then insert ldq
Is the problem ie. losing my existing data, due to the erasing of the
deque member? Am I working with the same object all the time?
Originally I had been adding extra data then just exiting but the local
deque was cleaned up and my data lost also.
Hope someone can clear this up.
TIA
Craig
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Wed Jul 21, 2004 7:13 pm Post subject: Re: using STL deque and structures |
|
|
Craig wrote:
| Quote: | I have an application that uses a deque of deques to hold information
from an NDS tree. Each deque within the deque represents a layer within
the tree and the elements of the deque are structs containing object data.
I have a situation where I need to add more information to an existing
deque but seem unable to come up with a solution that works.
snip |
The problem is that STL containers directly contain their elements
rather than just holding pointers to them. Moving a node from one
container to another, or within one container, can involve copying it
and then destroying the original (though this is not true of lists).
You can avoid this repeated copying and destruction by using
containers of pointers to nodes, but that requires very careful memory
management because the containers will only manage the memory used for
the pointers and not the nodes. Instead you can use containers of
appropriate smart pointers (such as boost::shared_ptr) or special
containers from outside the standard library. There has been a
proposal for a Boost pointer-container library but I don't know what
its status is now.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Jul 23, 2004 11:54 pm Post subject: Re: using STL deque and structures |
|
|
Craig <benbowcSpamNotI (AT) attglobal (DOT) net> wrote
| Quote: | Just for clarity this is what I do:
create local deque
if its an existing level we are getting extra data for
|
Right here is the problem:
| Quote: | ldq = StorageDeque.at(level);
|
Don't copy the deque out of the container. Instead, use a reference
to the existing deque.
This might be instructive:
typedef deque<int> di;
typedef deque<di> ddi;
int main() {
ddi myddi;
// Here, I'm populating the deque of deques
int xx=0;
for (int i=0; i<10; ++i) {
// Create a new deque
di mydi;
// Populate it
for (int j=0; j<4; ++j)
mydi.push_back(++xx);
// Add it to the deque of deques
myddi.push_back(mydi);
}
// Now we'll modify two of the contained deques.
{ // We use a local block to limit the scope.
// First, the deque at 5
di &d5 = myddi.at(5);
// Note that d3 isn't a copy of that deque...
// it's a reference to the one IN the container.
// Now modify it.
d5.push_back(++xx);
d5.push_back(++xx);
d5.push_back(++xx);
}
// Now do the same thing to the deque at 3
{ di &d3 = myddi.at(3);
d3.push_back(++xx);
d3.push_back(++xx);
}
// This just dumps the data to show how it worked
i=0;
for (ddi::iterator x=myddi.begin(); x!=myddi.end(); ++x) {
cout << "myddi(" << i++ << "):";
for (di::iterator y=x->begin(); y!=x->end(); ++y)
cout << ' ' << *y;
cout << endl;
}
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|