如何解决映射代理迭代器冲突 C++20 迭代器概念
我正在尝试编写一个包装类,它将 std::vector<T>
解释为整数到 T
的映射。因为我希望它表现得像一个 map
,所以它的迭代器应该取消对 (key,value)
对的引用。由于内存中没有这样的对,我对应的迭代器类的 reference
类型有点不合常规:
using value_type = std::pair<Key,Value>;
using reference = std::pair<Key,Value&>;
对于对应的const_iterator
,这变成
using value_type = std::pair<Key,const Value>;
using reference = std::pair<Key,const Value&>;
然而,现在我的 const_iterator
不再满足 std::indirectly_readable
(因此,std::forward_iterator
和 std::random_access_iterator
),因为
no type named 'type' in 'struct std::common_reference<std::pair<long unsigned int,const int&>&&,std::pair<long unsigned int,const int>&>'
(参见 https://godbolt.org/z/s89z56rY7)或此处的完整代码:
#include <iostream>
#include <type_traits>
#include <vector>
#include <iterator>
using namespace std;
template<class Value,class VType,class RType>
struct _vector_map_iterator {
Value* _data = nullptr;
size_t index = 0;
using value_type = VType;
using reference = RType;
using difference_type = ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;
reference operator*() const { return {index,*_data}; }
_vector_map_iterator& operator++() { ++index; return *this; }
_vector_map_iterator operator++(int) {_vector_map_iterator res = *this; ++(*this); return res; }
bool operator==(const _vector_map_iterator& other) const { return _data == other.data; }
};
template<class T>
using vmap_iterator = _vector_map_iterator<T,pair<size_t,T>,T&>>;
template<class T>
using vmap_const_iterator = _vector_map_iterator<const T,const T>,const T&>>;
using I = vmap_const_iterator<int>;
//static_assert(std::common_reference_with<typename I::reference&&,typename I::value_type&>);
static_assert(forward_iterator<I>);
template<class Value>
struct vector_map {
vector<Value> _raw_data;
using iterator = vmap_iterator<Value>;
using const_iterator = vmap_const_iterator<Value>;
iterator begin() { return {_raw_data.data(),0}; }
const_iterator begin() const { return {_raw_data.data(),0}; }
vector_map(const initializer_list<Value>& lst): _raw_data(lst) {};
};
int main(){
const vector_map<int> int_map = {1,2,3};
const auto it = int_map.begin();
cout << (*it).first << ": " << (*it).second << '\n';
}
我的问题是:对于这样一个也遵守 std::random_access_iterator
的迭代器类,是否有一个明智的(我特别不想在向量中存储键值对!)设计?
编辑:更多细节+完整示例
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。