C++ 运算符->用于返回临时值的迭代器
我有一个基本上是哈希表的数据结构。由于缓存原因,键和值分别存储。我有一个迭代器,它遍历容器并在取消引用时返回成对的键和值 然而,我在让迭代器像其他迭代器一样工作时遇到了一些困难。尤其是运算符->。以下是我目前掌握的情况:C++ 运算符->用于返回临时值的迭代器,c++,iterator,c++17,C++,Iterator,C++17,我有一个基本上是哈希表的数据结构。由于缓存原因,键和值分别存储。我有一个迭代器,它遍历容器并在取消引用时返回成对的键和值 然而,我在让迭代器像其他迭代器一样工作时遇到了一些困难。尤其是运算符->。以下是我目前掌握的情况: struct KeyValuePair { KeyValuePair(const int& key, int& value) : key(key), value(value) {} const int& key; int&
struct KeyValuePair
{
KeyValuePair(const int& key, int& value) : key(key), value(value) {}
const int& key;
int& value;
};
struct Iterator
{
KeyValuePair operator*() { return KeyValuePair(*keys, *values); }
// TODO: Implement this
//KeyValuePair* operator->() { return ... }
int* keys = nullptr;
int* values = nullptr;
};
这对于范围和显式取消引用迭代器非常有效
auto it = container.begin();
KeyValuePair pair = *it;
但它不适用于“通过”迭代器,因为我没有运算符->
我不知道如何为这个迭代器实现操作符->。我的第一个想法是在迭代器中粘贴一个KeyValuePair并返回指向该迭代器的指针,但是如果没有恶作剧,引用就无法重置
来自比我聪明的人的任何提示?迭代器有时会返回一个临时对象作为引用,但操作符->通常需要一个左值来获取其指针。在这种情况下,为了让操作符->工作,我们应该提供指针的包装器:C++11版本 结构KeyValuePair { KeyValuePairconst int&key,int&value:keykey,valuevalue{} const int&key; int&value; 私人: 样板 结构辅助程序{ T参考; T*运算符->{return std::addressofref;} }; Helper get{return Helper{*this};} 友元结构迭代器; }; 结构迭代器 { KeyValuePair运算符*{返回KeyValuePair*键,*值;} 自动运算符->->decltype**this.get{ 返回**this.get; } int*keys=nullptr; int*值=nullptr; };
如果引用对象KeyValuePair是可复制构造的,则它可以工作。在Helper的构造函数中进行复制是必要的,因为临时KeyValuePair在返回包装后将被销毁。迭代器有时会返回一个临时对象作为引用,但operator->通常需要一个左值来获取其指针。在这种情况下,为了让操作符->工作,我们应该提供指针的包装器:C++11版本 结构KeyValuePair { KeyValuePairconst int&key,int&value:keykey,valuevalue{} const int&key; int&value; 私人: 样板 结构辅助程序{ T参考; T*运算符->{return std::addressofref;} }; Helper get{return Helper{*this};} 友元结构迭代器; }; 结构迭代器 { KeyValuePair运算符*{返回KeyValuePair*键,*值;} 自动运算符->->decltype**this.get{ 返回**this.get; } int*keys=nullptr; int*值=nullptr; };
如果引用对象KeyValuePair是可复制构造的,则它可以工作。必须在Helper的构造函数中进行复制,因为返回包装后,临时KeyValuePair将被销毁。如果无法从运算符->返回指针,请按值返回Helper类。使该类存储KeyValuePair并重载其运算符->以返回指向该对的指针 我的答案和RedFog的答案是一样的,但是我试着让代码不那么复杂
struct Iterator
{
KeyValuePair operator*() const
{
return KeyValuePair(*keys, *values);
}
class ArrowHelper
{
KeyValuePair value
public:
ArrowHelper(KeyValuePair value) : value(value) {}
KeyValuePair *operator->() const
{
return &value;
}
};
ArrowHelper operator->() const
{
return **this;
}
int* keys = nullptr;
int* values = nullptr;
};
如果无法从运算符->返回指针,请按值返回帮助器类。使该类存储KeyValuePair并重载其运算符->以返回指向该对的指针 我的答案和RedFog的答案是一样的,但是我试着让代码不那么复杂
struct Iterator
{
KeyValuePair operator*() const
{
return KeyValuePair(*keys, *values);
}
class ArrowHelper
{
KeyValuePair value
public:
ArrowHelper(KeyValuePair value) : value(value) {}
KeyValuePair *operator->() const
{
return &value;
}
};
ArrowHelper operator->() const
{
return **this;
}
int* keys = nullptr;
int* values = nullptr;
};
而是在迭代器构造函数中创建该对的实例?然后,运算符可以将指针和引用返回到非临时对象。并且,是否有理由创建自己的对结构,而不是使用STD::GONH.@ Apple程序员,我认为OPS StuttKyValuePoAIR比STD::我的意思是第一和第二可能意味着一切,也可能什么都没有。但是,类似的std::容器(例如std::map)也使用std::pair。因此,它不应该引起任何人的惊讶。而是在迭代器构造函数中创建一个对的实例?然后,运算符可以将指针和引用返回到非临时对象。并且,是否有理由创建自己的对结构,而不是使用STD::GONH.@ Apple程序员,我认为OPS StuttKyValuePoAIR比STD::我的意思是第一和第二可能意味着一切,也可能什么都没有。但是,类似的std::容器(例如std::map)也使用std::pair。所以,它不应该引起任何人的惊讶。