Help with adjacent_find()

I need a simple adjacent_find() to find the adjacent same letter in a randomly generated string
Is this simple enough?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>
#include <functional>
using namespace std;
int main()
{
    random_device seed;
    mt19937 rnd(seed());
    uniform_int_distribution<> dist('a', 'z');

    string s;
    generate_n(back_inserter(s), 100, bind(dist, rnd));

    std::cout << "randomly-generated string:\n" << s << '\n';

    auto p = adjacent_find(s.begin(), s.end());
    if(p == s.end())
        cout << "The string has no adjacent pairs\n";
    else
        cout << "The first adjacent pair is at position " << p - s.begin()
             << " and the repeated letter is " << *p << '\n';
}


online demo: http://liveworkspace.org/code/40SGdU$0
Last edited on
Topic archived. No new replies allowed.