c++ - const、span 和迭代器的麻烦
问题描述
我尝试编写一个按索引迭代容器的迭代器。AIt
和 aconst It
都允许更改容器的内容。AConst_it
和 aconst Const_it
都禁止更改容器的内容。
之后,我尝试span<T>
在容器上写一个。对于T
非 const 类型,两者都const span<T>
允许span<T>
更改容器的内容。两者都const span<const T>
禁止span<const T>
更改容器的内容。
代码无法编译,因为:
// *this is const within a const method
// But It<self_type> requires a non-const *this here.
// So the code does not compile
It<self_type> begin() const { return It<self_type>(*this, 0); }
如果我让构造函数It
接受const
容器,它看起来不正确,因为迭代器可以修改容器的内容。
如果我去掉方法的 const,那么对于非 const 类型 T,aconst span<T>
不能修改容器。
It
继承 fromConst_it
以允许在模板实例化期间从It
to隐式转换。Const_it
我在迭代器 ( ) 中使用指针而不是对的引用来const C* container_;
允许将一个迭代器分配给另一个迭代器。
我怀疑这里出了点问题,因为我什至想到:
但我不知道如何解决它。
测试:
#include <vector>
#include <numeric>
#include <iostream>
template<typename C>
class Const_it {
typedef Const_it<C> self_type;
public:
Const_it(const C& container, const int ix)
: container_(&container), ix_(ix) {}
self_type& operator++() {
++ix_;
return *this;
}
const int& operator*() const {
return ref_a()[ix_];
}
bool operator!=(const self_type& rhs) const {
return ix_ != rhs.ix_;
}
protected:
const C& ref_a() const { return *container_; }
const C* container_;
int ix_;
};
template<typename C>
class It : public Const_it<C> {
typedef Const_it<C> Base;
typedef It<C> self_type;
public:
//It(const C& container.
It(C& container, const int ix)
: Base::Const_it(container, ix) {}
self_type& operator++() {
++ix_;
return *this;
}
int& operator*() const {
return mutable_a()[ix_];
}
private:
C& mutable_a() const { return const_cast<C&>(ref_a()); }
using Base::ref_a;
using Base::container_;
using Base::ix_;
};
template <typename V>
class span {
typedef span<V> self_type;
public:
explicit span(V& v) : v_(v) {}
It<self_type> begin() { return It<self_type>(*this, 0); }
// *this is const within a const method
// But It<self_type> requires a non-const *this here.
// So the code does not compile
It<self_type> begin() const { return It<self_type>(*this, 0); }
It<self_type> end() { return It<self_type>(*this, v_.size()); }
It<self_type> end() const { return It<self_type>(*this, v_.size()); }
int& operator[](const int ix) {return v_[ix];}
const int& operator[](const int ix) const {return v_[ix];}
private:
V& v_;
};
int main() {
typedef std::vector<int> V;
V v(10);
std::iota(v.begin(), v.end(), 0);
std::cout << v.size() << "\n";
const span<V> s(v);
for (auto&& x : s) {
x = 4;
std::cout << x << "\n";
}
}
解决方案
要完成这项工作,有两个主要注意事项。第一的:
如果我让 It 的构造函数接受一个 const 容器,它看起来不正确,因为迭代器可以修改容器的内容。
不是真的,因为C
in yourtemplate<typename C> class It
不是实际的容器,而是. 换句话说,看看:span<V>
It<self_type> begin() const { return It<self_type>(*this, 0); }
这里的self_type
意思是const span<V>
,因此你要返回一个It<const span<V>>
. 因此,您的迭代器可以做任何可以用 a 完成的事情const span
- 但容器仍然是 non- const
。那么变量名container_
就不好说了。
对于不是 的类型
T
,const
两者都const span<T>
允许span<T>
更改容器的内容。两者都const span<const T>
禁止span<const T>
更改容器的内容。
另外,既然您希望const span
允许修改内容,那么您应该在其内部编写span
的是(注意const
):
int& operator[](const int ix) const {return v_[ix];}
// Removing the other `const` version:
// const int& operator[](const int ix) const {return v_[ix];}
澄清这两个位后,您就可以构建一个工作示例。这是一个基于您的代码并简化以解决手头问题的代码:
#include <vector>
#include <iostream>
template<typename S>
class It {
typedef It<S> self_type;
const S& span_;
int ix_;
public:
It(const S& span, const int ix)
: span_(span), ix_(ix) {}
self_type& operator++() {
++ix_;
return *this;
}
int& operator*() const {
return span_[ix_];
}
bool operator!=(const self_type& rhs) const {
return &span_ != &rhs.span_ or ix_ != rhs.ix_;
}
};
template <typename V>
class span {
typedef span<V> self_type;
public:
explicit span(V& v) : v_(v) {}
It<self_type> begin() const { return It<self_type>(*this, 0); }
It<self_type> end() const { return It<self_type>(*this, v_.size()); }
int& operator[](const int ix) const {return v_[ix];}
private:
V& v_;
};
int main() {
typedef std::vector<int> V;
V v(10);
const span<V> s(v);
for (auto&& x : s) {
x = 4;
std::cout << x << "\n";
}
}
看看正确的实现operator!=
和 不需要非const
版本的begin()
and的事实end()
。你也可以在那里扔一个cbegin()
and cend()
。然后,您必须重新添加 const 迭代器案例。
顺便说一句,以防它为任何人节省一些混乱:在不久的将来,可能会添加std::span
(为 C++20 提出的);它只是一(pointer-to-first-element, index)
对——而不是你的(pointer-to-container, index)
版本。
换句话说,作为它的模板参数,它将采用元素的类型,而不是容器:
span<std::vector<int>> s(v);
// vs
std::span<int> s(v);
这使消费者std::span
可以避免知道幕后是哪个容器(甚至没有容器:连续的内存区域或数组)。
最后,您可能想看看GSL 的 implementation ofstd::span
以获得一些关于如何完全实现它的灵感(包括关于范围的第二个模板参数)。
推荐阅读
- javascript - 在 useEffect 反应钩子中的计时器期间更新数组
- jmeter - 如何在grafana中显示ThreadName?
- php - Laravel 7.14 将 cart_id 同步/附加到 product_id 一对多关系
- ruby-on-rails - webpacker Rails 6 中推荐了多少包?
- coldfusion - cfset 标签中的 cfoutput
- python - 如何使用 Selenium 和 Python 提取第一个搜索结果的 href 属性
- c# - 表单和 DLL 之间的 C# 值重置
- google-cloud-platform - 您如何覆盖 Google AI 平台的标准库(即升级 scikit-learn)并为自定义预测例程安装其他库?
- bash - 如何在本地机器上的每次 git 提交后自动执行 mvn 验证?
- c - 空函数的大小