gpt4 book ai didi

c++ - 带有移动迭代器的独特算法

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:34:21 26 4
gpt4 key购买 nike

是否允许使用 std::unique通过 std::make_move_iterator 创建的迭代器功能?我试过 the following , 并获得成功:

#include <iostream>
#include <ostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <iterator>

#include <cstdlib>

struct A
{

A() : i(std::numeric_limits< double >::quiet_NaN()) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(double ii) : i(ii) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(A const & a) : i(a.i) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(A && a) : i(std::move(a.i)) { std::cout << __PRETTY_FUNCTION__ << "\n"; a.i = std::numeric_limits< double >::quiet_NaN(); }
A & operator = (A const & a) { std::cout << __PRETTY_FUNCTION__ << "\n"; i = a.i; return *this; }
A & operator = (A && a) { std::cout << __PRETTY_FUNCTION__ << "\n"; i = std::move(a.i); a.i = std::numeric_limits< double >::quiet_NaN(); return *this; }
bool operator < (A const & a) const { std::cout << __PRETTY_FUNCTION__ << "\n"; return (i < a.i); }
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
bool operator == (A const & a) const { std::cout << __PRETTY_FUNCTION__ << "\n"; return (i == a.i); }
#pragma clang diagnostic pop

friend
std::ostream &
operator << (std::ostream & o, A const & a)
{
return o << a.i;
}

private :

double i;

};

int
main()
{
std::vector< A > v{1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 4.0, 4.0, 5.0, 6.0, 6.0, 7.0};
std::cout << "v constructed\n\n\n\n";
std::sort(v.begin(), v.end());
auto const end = std::unique(std::make_move_iterator(v.begin()), std::make_move_iterator(v.end())).base();
std::copy(v.begin(), end, std::ostream_iterator< A >(std::cout, " "));
std::cout << std::endl;
return EXIT_SUCCESS;
}

但也许它是依赖于实现的成功吗?

关于 <numeric> 中的其他算法呢?和 <algorithm>

最佳答案

程序保证按标准运行。
std::unique需要前向迭代器。显示移动迭代器满足该要求的最简单方法是检查 iterator_category move_iterator 的类型定义:

typedef typename iterator_traits<Iterator>::iterator_category iterator_category;

可以看到,直接适配了底层迭代器类型的迭代器类别。事实上,移动迭代器的行为几乎等同于它们的底层行为,[move.iterators]/1:

Class template move_iterator is an iterator adaptor with the same behavior as the underlying iterator except that its indirection operator implicitly converts the value returned by the underlying iterator’s indirection operator to an rvalue reference.

没有其他值得注意的要求:显然vector<>::iterator是一个输入迭代器(根据 [move.iter.requirements] 的要求)。 unique施加的唯一相关要求本身就是

The type of *first shall satisfy the MoveAssignable requirements (Table 22).

...这是直截了当的。

请注意,使用移动迭代器不应比普通迭代器带来任何优势。在内部,重复元素是移动分配的(因此有 MoveAssignable 要求),因此从 operator* 返回右值引用是多余的。

关于c++ - 带有移动迭代器的独特算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28199082/

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