secretgenes secretgenes - 1 year ago 131
C++ Question

How to store a const char* [] in std :: string?

const char * tag1[]={"abc","xyz"};

how to convert tag1 in std::string?

std::string tag2(tag1)

it simply copies tag1[0] to tag2. what i want is to convert the whole tag1 array into string.

Any ideas or workaround?

Answer Source

Since you want to merge array of C-strings into one std::string, you can do this:

std::string tag2;   
for (auto ptr : tag1) {

Iterate over C-strings array and append them to destination std::string.

Direct copy of tag1 array to pre-allocated std::string won't work because of null-terminators at end of each C-string in tag1. You will get abc\0xyz, instead of abcxyz

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download