String manipulation with std::adjacent_find For the given strings (not containing numbers), print their shortened
  versions,  where each adjacent sequence of the same characters longer
  than 2, change to an expression consisting of a sign and a number of
  repetitions.


Sample input:

4
AAA
ABCDEF
CCCCCCDDDDDDD
ZZAACCCDDDDEEEEEE


Sample output:

A3
ABCDEF
C6D7
ZZAAC3D4E6


My code:

#include &lt;algorithm&gt;
#include &lt;cstddef&gt;
#include &lt;functional&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;

std::string reduce(std::string const&amp; word) {
    std::string result;
    for (auto it = word.cbegin(); true;) {
        auto curr = std::adjacent_find(it, word.cend(), std::not_equal_to&lt;int&gt;());
        auto dist = std::distance(it, curr) + (curr != word.cend());

        if (dist &lt; 3) {
            result += std::string(dist, *it);
        } else {
            result += *it + std::to_string(dist);
        }

        if (curr == word.cend()) {
            break;
        }
        it = 1 + curr;
    }
    return result;
}

int main() {
    std::size_t tests;
    std::cin &gt;&gt; tests;
    while (tests--) {
        std::string word;
        std::cin &gt;&gt; word;
        std::cout &lt;&lt; reduce(word) &lt;&lt; "\n";
    }
}


How could I simplify or improve this code?
First impressions: nicely presented code; good use of the appropriate standard library functions and classes.

A minor suggestion would be to change the name, given that reduce is a well-known concept in functional programming (and is a function in &lt;numeric&gt;).  Perhaps call it compress?

I'd suggest extracting the constant 3 to give it a meaningful name.

Can we eliminate the break with some reorganisation of the loop?  Perhaps by using std::mismatch(it, it+1) instead of std::adjacent_find()?  (I haven't fully thought that through; it might not be better.)

We can avoid constructing a new string here:


        result += std::string(dist, *it);



by using the overload of append() that takes two iterators:

        result.append(dist, *it);