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)