我想创建一个自定义迭代器包装器,例如 enumerate
: 给定一对类型为 T
的迭代器, 它会返回一个类型为 std::pair<const int, T&>
的可迭代对象,其中该对的第一个元素将取值 0、1、2,依此类推。
我无法确定应该是什么 value_type
和 reference
我的迭代器。我想支持两种行为:
首先,引用底层序列的值:
for (auto& kv: enumerate(my_vec)) {
kv.second = kv.first;
}
(类似于 std::iota
);
其次,复制值:
std::vector<int> a{10, 20, 30};
auto copy = *enumerate(a).begin();
a[0] = 15;
std::cout << copy.first << " " << copy.second; // 0 10
我很困惑 Iterator::operator*()
的返回类型应该是什么.如果是std::pair<const int, T&>
那么在第二个示例中,值将不会被复制。如果是std::pair<const int, T>
那么在第一个示例中,不可能引用基础值。我应该做什么,应该是什么value_type
, reference
和 pointer
这种迭代器的类型定义?
这是我尝试实现它的尝试。支持引用,不支持复制。
template<typename T>
struct Iterator {
using TT = typename std::iterator_traits<T>::value_type;
using value_type = std::pair<const int, TT>;
using reference = std::pair<const int&, typename std::iterator_traits<T>::reference>;
using pointer = value_type*;
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
std::pair<int, T> it;
Iterator(T iterator) : it(0, iterator) {}
bool operator==(const Iterator& other) const { return it.second == other.it.second; }
bool operator!=(const Iterator& other) const { return it.second != other.it.second; }
reference operator*() { return { it.first, *it.second }; }
Iterator& operator++() { ++it.first; ++it.second; return *this; }
};
附言我刚刚检查过,boost::adaptors::index 遇到同样的问题并且没有复制值。
最佳答案
这个问题和std::vector<bool>
的问题类似,你想提供一个代理,它的行为就像一个引用,但也支持值语义。
但不同的是,所涉及的类型不受限制,涉及两个引用,并且会弹出各种毛羽。以下是部分实现,它说明了您遇到的一些问题
#include<iterator>
#include<functional>
template<typename F, typename S, bool defined = true>
struct sfinae_difference_type {};
template<typename F, typename S>
struct sfinae_difference_type<F, S,
std::is_same_v<typename std::iterator_traits<F>::difference_type,
typename std::iterator_traits<S>::difference_type>>
{
using difference_type = typename std::iterator_traits<F>::difference_type;
};
template<typename F, typename S>
class pair_iterator : sfinae_difference_type<F, S>
{
using Fvalue_type = typename std::iterator_traits<F>::value_type;
using Svalue_type = typename std::iterator_traits<S>::value_type;
using Freference = typename std::iterator_traits<F>::reference;
using Sreference = typename std::iterator_traits<S>::reference;
F f;
S s;
public:
using value_type = std::pair<Fvalue_type, Svalue_type>;
struct reference
{
Freference first;
Sreference second;
reference() = delete;
reference(const reference& other) : first{other.first}, second{other.second} {}
reference& operator=(const reference& rhs)
{
first = rhs.first;
second = rhs.second;
return *this;
}
operator value_type() { return {f, s}; }
private:
reference(Freference f, Sreference s) : first{f}, second{s} {}
friend pair_iterator;
};
struct pointer
{
// similar to reference
};
pair_iterator() = default;
pair_iterator(const pair_iterator&) = default;
pair_iterator(F f, S s) : f{f}, s{s} {}
pair_iterator& operator++() { ++f; ++s; return *this; }
reference operator*() { return {*f, *s}; }
pointer operator->() { return {f.operator->(), s.operator->()}; }
bool operator==(const pair_iterator& other)
{
return f == other.f && s == other.s;
}
};
然后您将其用作
#include<vector>
#include<list>
#include<iostream>
int main()
{
std::vector v{1, 2, 3, 4, 5};
std::list l{6, 7, 8, 9, 10};
pair_iterator begin{v.begin(), l.begin()}, end{v.end(), l.end()};
for(; begin != end; ++begin)
std::cout << begin->first << ' ' << begin->second << '\n';
}
一些立即显而易见的问题:
- 实现很乏味。拥有 sfinae 友好的类型别名和适当的代理需要大量的样板。
- 代理的语义可能令人困惑。什么是复制/分配一个
reference
另一个意思?什么是auto is_this_a_copy = *it
应该做什么? - 平等是什么意思?两个内部迭代器是否必须相等才能相等?这打破了与结束迭代器的比较。
所有这些都必须敲定才能使其发挥作用,而且没有一个简单的答案。
关于c++ - 具有复杂值类型 : confusion with value_type and reference 的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49096829/