简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM