C ++ strings associated with char arrays

Is this a good way to do this?

char* array = "blah blah"; char* array2 = "bloh bloh"; string str = string() + array + array2; 

It is impossible to make a direct string str = array + array2 , cannot add 2 pointers. Or should I do it

 string str(); str += array; str += array2; 
+4
source share
2 answers

I would write:

 string str = string(array) + array2; 

Please note that your second version is invalid code . You must remove the parentheses:

 string str; str += array; str += array2; 

Finally, array and array2 must be of type const char * .

+4
source

There are many ways to do this:

 string str(array); str += array2; 

or

 string str = string(array) + array2; 

or even

 string str = array + string(array2); 

or string streams:

 stringstream ss; ss << array << array2; 
+3
source

Source: https://habr.com/ru/post/1411586/


All Articles