arrays - C++ string appending problems -


i'm having issues right now, attempting append char array onto c++ string after setting of values of c++ string, , don't see why. wondering if of know what's going.

here's code i'm trying run:

string test = ""; test.resize(1000); char sample[10] = { "hello!" }; test[0] = '1'; test[1] = '2'; test[2] = '3'; test[3] = '4'; test += sample; 

running through debugger, seems test "1234", , "hello" never added.

thanks in advance!

it added, after 1000 characters have in string (4 of them 1234, , 996 '\0' characters)`.

the resize function allocate 1000 characters string object, sets length 1000. that's why want instead use reserve

this do:

string test = ""; test.reserve(1000); // length still 0, capacity: 1000 char sample[10] = { "hello!" }; test.push_back('1'); // length 1 test.push_back('2'); // length 2 test.push_back('3'); // length 3 test.push_back('4'); // length 4 test += sample; // length 10 

or if want way:

string test = ""; test.resize(1000); // length 1000 char sample[10] = { "hello!" }; test[0] = '1'; // length 1000 test[1] = '2'; // length 1000 test[2] = '3'; // length 1000 test[3] = '4'; // length 1000 test.resize(4); // length 4, internal buffer still has capacity of 1000 characters test += sample; // length 10 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -