“模板<> int line< 0> ::操作员[](int y)const"做?
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1e9 + 7, maxn = 2e6;
int N, M, p[1 << 10], buf[maxn];
template <bool t> struct line {
int *v;
int operator[](int y) const;
};
template <> int line<0>::operator[](int y) const { return v[y]; }
template <> int line<1>::operator[](int y) const { return v[M * y]; }
这是什么操作员?是功能吗?如果是这样,为什么它具有正方形括号和之后的“ const”? 这些模板也意味着什么?我假设它执行其中一个 取决于t的价值(true或false)'
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1e9 + 7, maxn = 2e6;
int N, M, p[1 << 10], buf[maxn];
template <bool t> struct line {
int *v;
int operator[](int y) const;
};
template <> int line<0>::operator[](int y) const { return v[y]; }
template <> int line<1>::operator[](int y) const { return v[M * y]; }
What is this operator thing? Is it a function? If it is then why does it have square brackets and "const" after it?
Also do these template things mean? I'm assuming that it executes one of them
depending on the value of t ( true or false )'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您会声明
operator []
作为类模板的成员函数,名为line
。通过提供此功能,我们说我们超载operator []
我们的类模板line
line (实际上是针对特定类型的类型实例化)。const
表示此operator []
成员函数是 const成员函数 。这意味着我们不允许更改此成员功能中的非静态数据成员。假设您正在询问
template&lt;&gt;
正如问题标题所建议的那样,这意味着您明确(充分)专门化 构件函数operator []
对于不同的类 - 拼写参数<代码> 0 和1
。更多详细信息可以在任何好的C ++书籍中找到。
另请参阅为什么我不应该#include #include #include&lt; bit; ;?。
You're declaring
operator[]
as a member function of the class template namedline
. By providing this, we say that we're overloadingoperator[]
for our class templateline
(really for a specific class type that will be instantiated).The
const
means that thisoperator[]
member function is a const member function. Meaning we're not allowed to change the non-static non-mutable data members inside this member function.Assuming you are asking about the
template<>
as the question title suggests, it means that you're explicitly(fully)specializing the member functionoperator[]
for different class-template arguments0
and1
.More details can be found in any of the good C++ books.
Also refer to Why should I not #include <bits/stdc++.h>?.