gpt4 book ai didi

c++ - 自定义迭代器的 cbegin 未被使用

转载 作者:行者123 更新时间:2023-11-27 22:47:17 26 4
gpt4 key购买 nike

我已经在映射的键上定义了两个迭代器:

template<class MyMap>
struct MapKeyIterator : MyMap::iterator {
using Base = typename MyMap::iterator;
using Key = typename MyMap::key_type;
MapKeyIterator() : Base(){};
MapKeyIterator(Base it_) : Base(it_){};

Key *operator->() const { return &(Base::operator->()->first); }
Key operator*() const { return Base::operator*().first; }
};

template<class MyMap>
struct MapKeyConstIterator : MyMap::const_iterator {
using Base = typename MyMap::const_iterator;
using Key = typename MyMap::key_type;
MapKeyConstIterator() : Base(){};
MapKeyConstIterator(Base it_) : Base(it_){};

Key *operator->() const { return &(Base::operator->()->first); }
Key operator*() const { return Base::operator*().first; }
};

以下类型使用这些迭代器:

struct A {
using MyMap = std::map<int, int>;
using KeyIterator = MapKeyIterator<MyMap>;
using KeyConstIterator = MapKeyConstIterator<MyMap>;

KeyIterator begin() { return m.begin(); }
KeyConstIterator cbegin() const { return m.cbegin(); }
KeyIterator end() { return m.end(); }
KeyConstIterator cend() const { return m.cend(); }
private:
MyMap m{{1, 2}, {2, 4}, {3, 6}, {4, 8}};
};

以下不编译:

void f(const A &a) {
for (const auto &el: a)
std::cout << el << std::endl;
}

int main() {
A a;
f(a);
return 0;
}

g++ 5.4.0 的错误消息表明使用了 begin 而不是 cbegin。为什么?

temp.cpp: In function ‘void f(const A&)’:
temp.cpp:68:26: error: passing ‘const A’ as ‘this’ argument discards qualifiers [-fpermissive]
for (const auto &el: a)
^
temp.cpp:59:17: note: in call to ‘A::KeyIterator A::begin()’
KeyIterator begin() { return m.begin(); }
^
temp.cpp:68:26: error: passing ‘const A’ as ‘this’ argument discards qualifiers [-fpermissive]
for (const auto &el: a)
^
temp.cpp:61:17: note: in call to ‘A::KeyIterator A::end()’
KeyIterator end() { return m.end(); }

最佳答案

基于范围的for 从不 调用cbegin/cend。这就是为什么您仍然需要返回 const_iteratorconst 版本的 begin/end

关于c++ - 自定义迭代器的 cbegin 未被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536713/

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