tags:

views:

260

answers:

7

I want to write a c array into a container and i prefer to modify it if possible. I was thinking of using vector but it does seem to have a write(*pchararray, len); function. String looked like the next best thing but that too doesnt have a write function?

A: 

Not sure exactly what you mean with your "write" comment but

std::vector<char> charBuffer;

is functionally equivalent to a c char(byte) array

&charBuffer[0]

gives you the contiguous underlying memory.

So now you can do

charBuffer.resize(100);
memcpy(&charBuffer[0], src, charBuffer.size());
Doug T.
Nice to see Doug T enhancing his answer by copying my wording and example! Now thats what I call community spirit...
jkp
@jkp: Looks like he /helped/ you.
Geoffrey Chetwood
Erm - I'm not sure how that is RichB: he changed the wording of his answer and added an example showing a resize and a copy: which were both on my answer. Thanks for the downvote.
jkp
I'm sure no one else had ever thought of such an example... Treating a vector as an array by taking the address of the first element, who'd have thought of it? You should have patented it while you had the chance. ;)That said, I actually prefer your answer. std::copy should be preferred over memcpy
jalf
I prefer jkp's answer, but I don't see anything in this one to be at all upset about. The example you both use is bloody obvious, and there's no crime in editing your post to improve the wording or add more detail. Quite the contrary.
jalf
@jalf: I totally agree: what annoyed me was the fact he posted his answer, then read mine and went and eddited his to add extra details gleamed from someone elses in order to earn the points! Not as if he dont have enough...now I know how he got them.
jkp
It's a decent solution in this case. But I usually steer clear of memcpy, as it is inherited c function and may easily introduce more issues than it solves if you're not careful. I was burned by it many times, especially while porting from 32 to 64 bit machines.
Hmm, I don't think the copy in your example registered to me. I did edit yours to add the <char> to improve your answer, without realizing the copy was there. I wouldn't have done this if I wasn't acting in good faith. I honestly just edited mine out of my own independent thought.
Doug T.
Anyway, I'm happy to not accept any rep and give it to jkp. His answer is just as good. In my mind, my thought process was to give a C lib example of how a vector is useful similar to the C like write function provided by the asker.
Doug T.
+1  A: 

You can use a vector. Preallocate the size and then address the first byte: vectors are guarnateed to be contiguous so this is always valid.

std::vector<char> vBuffer;
vBuffer.resize(nLength);
std::copy(pStart, pEnd, &vBuffer[0]);
jkp
+7  A: 

Given

char myarray[10];

You can use an STL iterator:

vector <char> v;
copy(myarray, myarray + 10, back_inserter(v));

You can use a constructor:

vector <char> v(myarray, myarray + 10);

You can resize and copy:

vector<char> v(10);
copy(myarray, myarray + 10, v.begin());

(and all these work similarly for string)

Thanks to comments/other answers :)

Jesse Beder
Since the size is known, I'd prefer to resize the vector first, and then copy straight into the vector, rather than using back_insert. Just to avoid needless resizing and copying.
jalf
Why not construct a vector using the two-iterator constructor? It is way less verbose.
Brian Neal
Optimization: To prevent unnecessary reallocation from the repeated push_back()s, reserve(sizeof(myarray)/sizeof(myarray[0])) before copy()-ing
Éric Malenfant
+2  A: 

Container selection depends on your usage of the individual elements. If you mean copying a c character array to a conatiner, this is what you can use:

char buf[ n ];
std::vector<char> vc(n); // Thanks to Éric 
std::copy(buf, buf + n, vc.begin());
dirkgently
This code invokes undefined behavior. You have to make sure that vc.size() == n, or use a back_inserter.
Éric Malenfant
Right, thanks Eric.
dirkgently
+3  A: 

Vector, string, and many other containers have a "two iterator" constructor for this purpose:

char raw_data[100];
std::vector<char> v(raw_data, raw_data + 100);
std::string s(raw_data, raw_data + 100);
Brian Neal
A: 

you should prefer using memcpy in this case (since the vector contains a POD type) over using std::copy, it's ALOT faster.

A: 

You can use:

string s( pchararray, len );  // constructor

Or:

string s;  s.append( pchararray, len ); // append example.

Or string::insert() if you wanted.

String happily handles null characters ('\0'), provided you specify the length.

See http://www.cplusplus.com/reference/string/string/

Mr.Ree