jeudi 23 juin 2016

Is which element is deleted defined by std::unique?

Based on the example code here I wrote this small example (ideone Link):

#include <iostream>
#include <algorithm>
#include <string>

int main() 
{
    std::string s = "foo123bar456wibble";
    auto end = std::unique(s.begin(), s.end(), [](char l, char r){
        return std::isdigit(l) && std::isdigit(r);
    });
    // What does s hold?
    std::cout << std::string(s.begin(), end) << '\n';
}

My output is:

foo1bar4wibble

Does the standard guarantee this behaviour, or would this also be acceptable?

foo2bar6wibble

The linked cppreference page says:

Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten.

But is that normative text or just a suggested implementation?

Aucun commentaire:

Enregistrer un commentaire