Efficiently creating a vector<char> which is a copy of a char[]

S

Steven Reddie

I have a large char[] which I'd like to copy a portion of into a
vector<char>. I can't seem to locate a simple method for doing so. I
assume involving iterators will not result in memcpy not being used
and therefore will be less than optimal. Can anyone help me out?

Regards,

Steven
 
A

Alf P. Steinbach

I have a large char[] which I'd like to copy a portion of into a
vector<char>. I can't seem to locate a simple method for doing so. I
assume involving iterators will not result in memcpy not being used
and therefore will be less than optimal. Can anyone help me out?

Check the std::vector constructors for constructing a vector from a
character array.

Check the resizing member functions and std::copy for copying into
an existing vector.
 
J

Jonathan Turkanis

Steven Reddie said:
I have a large char[] which I'd like to copy a portion of into a
vector<char>. I can't seem to locate a simple method for doing so. I
assume involving iterators will not result in memcpy not being used
and therefore will be less than optimal. Can anyone help me out?

A good implementation of std::copy will use memcpy, memmove or
something similar in the case you describe. You could also use
char_traits<char>::copy.

Jonathan
 
T

tom_usenet

I have a large char[] which I'd like to copy a portion of into a
vector<char>. I can't seem to locate a simple method for doing so. I
assume involving iterators will not result in memcpy not being used
and therefore will be less than optimal. Can anyone help me out?

std::vector<char> v(chararray, chararray + sizeof(chararray));

will collapse to a memcpy or memmove call on most (all?) vector
implementations I know of. Trace in with a debugger to confirm.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
S

Steven Reddie

tom_usenet said:
On 5 Feb 2004 21:28:39 -0800, (e-mail address removed) (Steven Reddie)
wrote:

std::vector<char> v(chararray, chararray + sizeof(chararray));

will collapse to a memcpy or memmove call on most (all?) vector
implementations I know of. Trace in with a debugger to confirm.

Thanks guys. It looks like I'm misreading the docs. This last form
of constructor must be the one defined as:

template<class InputIterator> vector(InputIterator _First,
InputIterator _Last);

which implies that InputIterator for a vector<char> is or is
implicitly constructed from a char*. It's been a while since I've
used the STL that it seems that I'm forgetting some really basic
stuff.

Regards,

Steven
 
T

tom_usenet

Thanks guys. It looks like I'm misreading the docs. This last form
of constructor must be the one defined as:

template<class InputIterator> vector(InputIterator _First,
InputIterator _Last);

which implies that InputIterator for a vector<char> is or is
implicitly constructed from a char*.

That constructor is the one that matches. You end up calling:

vector<char>::vector<char*>(char* _First, char* _Last);

That templated constructor should dispatch to a memmove/memcpy
implementation, since char is a scalar and char* is a simple pointer
or the same type.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top