Initialize std::string with character array

J

Jim Langston

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
 
D

dasjotre

Jim said:
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
 
P

Pete Becker

dasjotre said:
Jim said:
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)
 
P

Pete Becker

Jim said:
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)
 

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