Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialize std::string with character array

Reply
Thread Tools

Initialize std::string with character array

 
 
Jim Langston
Guest
Posts: n/a
 
      11-21-2006
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


 
Reply With Quote
 
 
 
 
dasjotre
Guest
Posts: n/a
 
      11-21-2006

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

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      11-21-2006
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)
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-21-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Easiest way to initialize a string builder to a random number of the same character Reporter Java 1 05-18-2007 07:11 PM
initialize an array of elements that contain another array wenmang@yahoo.com C Programming 10 08-02-2006 04:52 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Initialize array using file i/o procedure/function? Brandon VHDL 5 09-29-2005 05:32 PM
How to initialize a big (String-)Array fast? Patrick Java 6 12-07-2004 02:28 PM



Advertisments