How do I do memcpy with std::string?
I tried:
But it doesnt work..Code:std::string test; test.append();
I want to do something like:
Code:char h[4]; size_t size = 12; memcpy(&h, &size, 4 );
How do I do memcpy with std::string?
I tried:
But it doesnt work..Code:std::string test; test.append();
I want to do something like:
Code:char h[4]; size_t size = 12; memcpy(&h, &size, 4 );
Append reference
Basically append takes parameters to know what to append.
I did:Originally Posted by anon
string.append(&h, 0, 4);
You need to use pointers to the first and last element of the range. The name of an array acts like a pointer to the first element of an array, so 'h' is a pointer. On the other hand, something like h[3] is a character. However, you can use pointer arithmetic to get a pointer to h[3].
Or, if you want to append the whole char array, you can simply do this:Code:char h[] = "cruel world"; string str = "hello"; str.append(h+4, h+10); cout<<str<<endl;
Code:char h[] = "cruel world"; string str = "hello "; str.append(h, 12); cout<<str<<endl;
Last edited by 7stud; 01-17-2007 at 10:49 AM.