Copying STL Lists

H

Harry Overs

Hi,
what is the easiest way to copy a STL list of BYTEs to a new list that will
then be used and maintained by an external class? The list that is to be
copied is very large about 25000 so the I need a very efficient solution.

many thanks,
 
V

Victor Bazarov

Harry said:
what is the easiest way to copy a STL list of BYTEs to a new list that will
then be used and maintained by an external class? The list that is to be
copied is very large about 25000 so the I need a very efficient solution.

Depending on whether you want to retain the original list or not, using
'list::swap' or 'std::copy' is probably what you should consider.

You could also read more about "move semantics" in c.l.c++.moderated, in
archives.

Victor
 
G

Gianni Mariani

Harry said:
Hi,
what is the easiest way to copy a STL list of BYTEs to a new list that will
then be used and maintained by an external class? The list that is to be
copied is very large about 25000 so the I need a very efficient solution.

(assuming typedef char BYTE)
std::list<BYTE> is not exactly the most efficient way to store bytes and
25000 bytes is really quite small on some more modern systems.

I would probably just use list::size() to find the size, allocate a
vector or array (whatever is needed) and use std::copy or std::copy_n.

If the performance was not good enough, I'd do a profile and figure out
what was using the the cpu and fix that. Doing anything else right now
is speculative.
 
K

Kai-Uwe Bux

Harry said:
Hi,
what is the easiest way to copy a STL list of BYTEs to a new list that
will
then be used and maintained by an external class? The list that is to be
copied is very large about 25000 so the I need a very efficient solution.

many thanks,

Why not use simple assignment? The operator=() actually will make a copy of
the list:

#include <list>
#include <iostream>

int main ( void ) {
std::list< int > a, c;
a.push_back( 1 );
std::list< int > b = a;
b.push_back( 2 );
c = a;
std::cout << c.size() << " " << b.size() << '\n';
}

As for efficiency, please profile your program first and tell us about your
findings. Otherwise it is hard to suggest solutions (in fact, it is hard to
tell whether there is a problem to begin with).


Best

Kai-Uwe Bux
 
F

Fernando Lagos

Harry Overs said:
Hi,
what is the easiest way to copy a STL list of BYTEs to a new list that will
then be used and maintained by an external class? The list that is to be
copied is very large about 25000 so the I need a very efficient solution.

many thanks,

with = or list::assign


Hope this help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top