gpt4 book ai didi

c++11 - std::unique 定义了删除哪个元素?

转载 作者:行者123 更新时间:2023-12-02 08:20:07 25 4
gpt4 key购买 nike

基于 example code here 我写了这个小例子 ( 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';
}

我的输出是:

foo1bar4wibble

标准是否保证这种行为,或者这是否也可以接受?

foo2bar6wibble

链接的 cppreference 页面说:

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

但那是规范性文本还是只是建议的实现?

此外,cplusplus.com says:

Removes all but the first element from every consecutive group of equivalent elements in the range [first,last).

但这又是规范的吗?

最佳答案

25.3.9 [alg.unique]/1

Effects: For a nonempty range, eliminates all but the first element from every consecutive group of equivalent elements referred to by the iterator i in the range [first + 1,last) for which the following conditions hold: *(i - 1) == *i or pred(*(i - 1), *i) != false.

关于c++11 - std::unique 定义了删除哪个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38005753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com