472,146 Members | 1,212 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

Initialize std::string with character array

Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o
Nov 21 '06 #1
3 19597

Jim Langston wrote:
Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o
std::string s(buffer, buffer+5);

in your case it will be a 1 character long string with
SOH character only

Nov 21 '06 #2
dasjotre wrote:
Jim Langston wrote:
>Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o

std::string s(buffer, buffer+5);

in your case it will be a 1 character long string with
SOH character only
Buffer(buffer, 5) constructs a string object that holds the first five
characters in buffer.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 21 '06 #3
Jim Langston wrote:
Is it possible to initialize a std::string with a character array, not
neccessarily null terminated? I.E. Given something like this:

char buffer[5];
buffer[0] = 0x01;
buffer[1] = 0x00;
buffer[2] = 'A';
buffer[3] = 'B';
buffer[4] = 'C';

The only way I know to do it now is to create a std::string and with a for
loop add each character. Is there some other, better way? I mean, I would
love to be able to say:

std::string Buffer( buffer, 5 );

Oh, you gotta be joking me! I just tried that just for the heck of it, and
it compiles and does exactly as I expect! O.o
No need to be surprised. Just read the documentation.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 21 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Alexandros Frantzis | last post: by
1 post views Thread by Christopher Benson-Manica | last post: by
7 posts views Thread by Dylan | last post: by
12 posts views Thread by Flzw | last post: by
8 posts views Thread by Jason Heyes | last post: by
7 posts views Thread by JustSomeGuy | last post: by
14 posts views Thread by Mosfet | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

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.