BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to 'move' a list-element without insert/erase?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
MR
Guest





PostPosted: Fri Feb 24, 2006 11:03 am    Post subject: How to 'move' a list-element without insert/erase? Reply with quote



<I want to move an element e of a list, pointed to by an iterator it1
to a position pointed to by it2.

I examined the member-functions and found erase, which removes the
element and insert which inserts the element.

The problem is that both of these functions do not use their own
new/delete-routine to deallocate/allocate space.

So: Are there functions that remove/add elements from/to a list

It's like using the DOS-command
copy c:\dir1\large_file.exe c:\dir2\large_file.exe
delete c:\dir1\large_file.exe
instead of
move c:\dir1\large_file.exe c:\dir2\large_file.exe

Beside the additional time, it seems, I have to implement a
copy-constructor, I have not needed before (if I do not want the
default-one is used). (And because my class contains visible visual
components, this would be quite difficult).

I'm sure there is a better solution for this, insn't it?

If not, I seem to have to create my own list...

Thanks a lot,

Michael
Back to top
Alan Bellingham
Guest





PostPosted: Fri Feb 24, 2006 11:03 am    Post subject: Re: How to 'move' a list-element without insert/erase? Reply with quote



MR <> wrote:

Quote:
I want to move an element e of a list, pointed to by an iterator it1
to a position pointed to by it2.

I examined the member-functions and found erase, which removes the
element and insert which inserts the element.

Yes.

Quote:
The problem is that both of these functions do not use their own
new/delete-routine to deallocate/allocate space.

They should use the ones that that particular container uses. (Almost
certainly the global one, unless you specifically instantiated the
container with a different allocator.) Or am I misunderstanding you?

Quote:
So: Are there functions that remove/add elements from/to a list

It sounds like you want to use splice(). Go see - it has three versions.

Alan Bellingham
--
ACCU Conference 2006 - 19-22 April, Randolph Hotel, Oxford, UK
Back to top
MR
Guest





PostPosted: Fri Feb 24, 2006 1:03 pm    Post subject: Re: How to 'move' a list-element without insert/erase? Reply with quote



Quote:
It sounds like you want to use splice(). Go see - it has three versions.

Yes, that was what I was looking for.


Thanks a lot for the quick answer..

Michael
Back to top
Gillmer J. Derge [TeamB]
Guest





PostPosted: Fri Feb 24, 2006 4:04 pm    Post subject: Re: How to 'move' a list-element without insert/erase? Reply with quote

MR wrote:
Quote:
Beside the additional time, it seems, I have to implement a
copy-constructor, I have not needed before (if I do not want the
default-one is used). (And because my class contains visible visual
components, this would be quite difficult).

If you're trying to avoid implementing a copy constructor for something
stored in one of the standard containers, don't. The standard says,
"The type of objects stored in these components must meet the
requirements of CopyConstructible types (20.1.3), and the additional
requirements of Assignable types."

It's convenient that it works given your current use, but you're leaving
the door open for other problems in the future. You may decide you need
some other function of lists that surprisingly requires a copy
constructor. Or the library implementation could change in an update,
and you'd be stuck. You're taking advantage of luck and undefined
behavior with this approach.

The more common solution to the copy constructible problem is to make a
container of pointers (smart pointers) instead of your actual object.
Pointers can always be copied and assigned, so you don't run into these
issues with them.

Having said that, you're still right to use splice instead of
insert/remove. I just think you should do it for efficiency reasons
rather than to avoid implementing the required copy constructor.

--
Gillmer J. Derge [TeamB]
Back to top
Chris Uzdavinis (TeamB)
Guest





PostPosted: Fri Feb 24, 2006 9:03 pm    Post subject: Re: How to 'move' a list-element without insert/erase? Reply with quote

"Gillmer J. Derge [TeamB]" <spam (AT) gillmerderge (DOT) com> writes:

Quote:
If you're trying to avoid implementing a copy constructor for
something stored in one of the standard containers, don't. The
standard says, "The type of objects stored in these components must
meet the requirements of CopyConstructible types (20.1.3), and the
additional requirements of Assignable types."

While I agree completely, you might really not want something copyable
but still want it in a standard container. A little indirection can
help here, and then use something like a boost::shared_ptr<YourType>
and you can then put them in a list. (Though in reality, they're in
something that is in the list.)

--
Chris (TeamB);
Back to top
Gillmer J. Derge [TeamB]
Guest





PostPosted: Fri Feb 24, 2006 10:03 pm    Post subject: Re: How to 'move' a list-element without insert/erase? Reply with quote

Chris Uzdavinis (TeamB) wrote:
Quote:
A little indirection can
help here, and then use something like a boost::shared_ptr<YourType
and you can then put them in a list.

Didn't I say that (without referencing Boost specifically)? ;-)

--
Gillmer J. Derge [TeamB]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.