从 begin() 而不是 cbegin() 获取 const_iterator
Getting const_iterator from begin() instead of cbegin()
有人可以解释为什么如果我取消注释该行,下面的代码将无法编译吗
foo::const_iterator j = f.begin();
,但如果我使用行 foo::const_iterator j = f.cbegin();
,它会编译吗?我试图让这条线像我的 std::vector
示例一样工作。
#include <vector>
struct foo {
struct node { };
node *first = nullptr, *last = nullptr;
struct base_iterator {
node* ptr;
base_iterator (node* n) : ptr(n) { }
};
struct iterator : base_iterator { using base_iterator::base_iterator; };
struct const_iterator : base_iterator { using base_iterator::base_iterator; };
iterator begin() { return iterator(first); }
const_iterator begin() const { return const_iterator(first); }
const_iterator cbegin() const { return const_iterator(first); }
};
// Test
int main() {
foo f;
foo::iterator i = f.begin();
// foo::const_iterator j = f.begin(); // Won't compile because f is not const.
// foo::const_iterator j = f.cbegin(); // Will compile fine.
std::vector<int> v;
std::vector<int>::const_iterator it = v.begin(); // Compiles even though v is not const.
}
它适用于 std::vector
,因为所有标准库容器的迭代器都是 设计 以支持 iterator
--> const_iterator
转换.它旨在模仿指针转换的工作方式。
只要你的两个迭代器是用户定义的类,你就需要明确地添加它。您有两个选择:
转换构造函数:
struct iterator : base_iterator { using base_iterator::base_iterator; };
struct const_iterator : base_iterator {
using base_iterator::base_iterator;
const_iterator(const iterator& other) : base_iterator(other) {}
};
一个转换运算符:
struct const_iterator : base_iterator { using base_iterator::base_iterator; };
struct iterator : base_iterator {
using base_iterator::base_iterator;
operator const_iterator() const { /* ... */ }
};
有人可以解释为什么如果我取消注释该行,下面的代码将无法编译吗
foo::const_iterator j = f.begin();
,但如果我使用行 foo::const_iterator j = f.cbegin();
,它会编译吗?我试图让这条线像我的 std::vector
示例一样工作。
#include <vector>
struct foo {
struct node { };
node *first = nullptr, *last = nullptr;
struct base_iterator {
node* ptr;
base_iterator (node* n) : ptr(n) { }
};
struct iterator : base_iterator { using base_iterator::base_iterator; };
struct const_iterator : base_iterator { using base_iterator::base_iterator; };
iterator begin() { return iterator(first); }
const_iterator begin() const { return const_iterator(first); }
const_iterator cbegin() const { return const_iterator(first); }
};
// Test
int main() {
foo f;
foo::iterator i = f.begin();
// foo::const_iterator j = f.begin(); // Won't compile because f is not const.
// foo::const_iterator j = f.cbegin(); // Will compile fine.
std::vector<int> v;
std::vector<int>::const_iterator it = v.begin(); // Compiles even though v is not const.
}
它适用于 std::vector
,因为所有标准库容器的迭代器都是 设计 以支持 iterator
--> const_iterator
转换.它旨在模仿指针转换的工作方式。
只要你的两个迭代器是用户定义的类,你就需要明确地添加它。您有两个选择:
转换构造函数:
struct iterator : base_iterator { using base_iterator::base_iterator; };
struct const_iterator : base_iterator {
using base_iterator::base_iterator;
const_iterator(const iterator& other) : base_iterator(other) {}
};
一个转换运算符:
struct const_iterator : base_iterator { using base_iterator::base_iterator; };
struct iterator : base_iterator {
using base_iterator::base_iterator;
operator const_iterator() const { /* ... */ }
};