 <p>Here's a fairly simple algorithm to overlap two strings:</p> <pre><code>#include &lt;string&gt; std::string overlap(std::string first, std::string const &amp; second) { std::size_t pos = 0; for (std::size_t i = 1; i &lt; first.size(); ++i) { if (first.compare(first.size() - i, i, second, 0, i) == 0) { pos = i; } } first.append(second, pos, second.npos); return first; } </code></pre> <p>Usage:</p> <pre><code>std::string result = overlap("abcde", "defgh"); </code></pre> <p>And to overlap a whole range, use <code>std::accumulate</code>:</p> <pre><code>#include &lt;algorithm&gt; #include &lt;iostream&gt; #include &lt;vector&gt; int main() { std::vector&lt;std::string&gt; strings = {"abc", "def", "fegh", "ghq"}; std::cout &lt;&lt; std::accumulate(strings.begin(), strings.end(), std::string(), overlap) &lt;&lt; std::endl; } </code></pre>