Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Remember, that, in case of arrays, sizeof(array) returns actual size (aka length) of it's parameter, if it has been declared as an array of explicit size (and you wrote 'series of const char*'s of unknown size'). So, assuming you want to create universal solution, strlen() should come under consideration instead.</p> <p>I don't think you can avoid all additional operations. In case of many concatenations, the best solution would be to allocate buffer, that is large enough to store all concatenated strings.</p> <p>We can easily deduce, that the most optimal version of append() in this case is:</p> <pre><code>string&amp; append (const char* s, size_t n); </code></pre> <p>Why? Because reference says: 'If <code>s</code> does not point to an array long enough (...), it causes undefined behavior'. So we can assume, that internally no additional checks take place (especially additional strlen() calls). Which is good, since you are completely sure, that values passed to append() are correct and you can avoid unnecesary overhead.</p> <p>Now, the actual concatenation can be done like this:</p> <pre><code>len_1 = strlen(array_1); len_2 = strlen(array_2); len_3 = strlen(array_3); //Preallocate enough space for all arrays. Only one reallocation takes place. target_string.reserve(len_1 + len_2 + len_3); target_string.append(array_1, len_1); target_string.append(array_2, len_2); target_string.append(array_3, len_3); </code></pre> <p>I do not know if this solution 'looks good' in your opinion, but it's definitely clear and is optimized for this use case.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload