“模板<> int line< 0> ::操作员[](int y)const"做?

发布于 2025-02-01 18:35:12 字数 509 浏览 2 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-02-08 18:35:12

这是什么操作员?是功能吗?如果是这样,为什么它具有方括号

您会声明operator []作为类模板的成员函数,名为line。通过提供此功能,我们说我们超载 operator []我们的类模板line line (实际上是针对特定类型的类型实例化)。


为什么它具有const之后

const表示此operator []成员函数是 const成员函数 。这意味着我们不允许更改此成员功能中的非静态数据成员。


这些模板也意味着什么?

假设您正在询问template&lt;&gt;正如问题标题所建议的那样,这意味着您明确(充分)专门化 构件函数operator []对于不同的类 - 拼写参数<代码> 0 和1


更多详细信息可以在任何好的C ++书籍中找到。

另请参阅为什么我不应该#include #include #include&lt; bit; ;?

What is this operator thing? Is it a function? If it is then why does it have square brackets

You're declaring operator[] as a member function of the class template named line. By providing this, we say that we're overloading operator[] for our class template line(really for a specific class type that will be instantiated).


why does it have const after it

The const means that this operator[] 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.


Also do these template things mean?

Assuming you are asking about the template<> as the question title suggests, it means that you're explicitly(fully)specializing the member function operator[] for different class-template arguments 0 and 1.


More details can be found in any of the good C++ books.

Also refer to Why should I not #include <bits/stdc++.h>?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文