473,320 Members | 1,846 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,320 software developers and data experts.

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

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
Jul 22 '05 #1
5 1861
On 5 Feb 2004 21:28:39 -0800, sm*@essemer.com.au (Steven Reddie) wrote:
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.

Jul 22 '05 #2
"Steven Reddie" <sm*@essemer.com.au> wrote in message
news:f9**************************@posting.google.c om...
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
Jul 22 '05 #3
On 5 Feb 2004 21:28:39 -0800, sm*@essemer.com.au (Steven Reddie)
wrote:
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
Jul 22 '05 #4
tom_usenet <to********@hotmail.com> wrote in message news:<gk********************************@4ax.com>. ..
On 5 Feb 2004 21:28:39 -0800, sm*@essemer.com.au (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
Jul 22 '05 #5
On 6 Feb 2004 19:14:52 -0800, sm*@essemer.com.au (Steven Reddie)
wrote:
tom_usenet <to********@hotmail.com> wrote in message news:<gk********************************@4ax.com>. ..
On 5 Feb 2004 21:28:39 -0800, sm*@essemer.com.au (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*.


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
Jul 22 '05 #6

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

Similar topics

3
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes...
2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
6
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
8
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ;...
4
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual...
6
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.