关于 C++ 的堆栈实现的一个快速问题标准库

发布于 2024-09-08 02:23:27 字数 434 浏览 1 评论 0原文

该行是什么:

template<typename _Tp1, typename _Seq1>
friend bool
operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);

http://gcc. gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.4/a01367.html

做什么?

为什么 _Tp1 在争论列表中重复两次? 谢谢,

What does the line:

template<typename _Tp1, typename _Seq1>
friend bool
operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);

in http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.4/a01367.html

do?

Why is _Tp1 repeated twice in arguements list?
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

居里长安 2024-09-15 02:23:27

这就像问为什么 in:

int strcmp( const char * a, const char * b );

const char * 重复两次 - 有两件事需要比较。 _Tp1 模板参数是存储在堆栈中的内容的类型 - 进行比较的两个堆栈必须存储相同的类型。

请注意,阅读标准库源代码不是学习 C++ 的好方法 - 您需要一本好书,例如 这个

That's like asking why in:

int strcmp( const char * a, const char * b );

const char * is repeated twice - there are two things to compare. The _Tp1 template parameter is the type of thing being stored in the stack - both stacks being compared must store the same type.

Please note that reading the Standard Library source is not a good way of learning C++ - you need a good book, such as this one.

夏尔 2024-09-15 02:23:27

它声明了两个stacksa 友元函数之间的相等运算符类,这是访问私有成员所必需的。

const stack<_Tp1, _Seq1> 出现两次,因为有 2 个参数。

当然,它可以写成,

bool operator==(const stack<_Tp1, _Seq1>& y) const { return c == y.c; }

但 C++ 标准 (§[stack.ops] (23.3.5.3.4)) 似乎要求此运算符是一个自由函数。

It declares the equality operator between two stacks a friend function of this class, which is necessary for it to access private members.

The const stack<_Tp1, _Seq1> appear twice because there are 2 arguments.

Of course it can be written as

bool operator==(const stack<_Tp1, _Seq1>& y) const { return c == y.c; }

but the C++ standard (§[stack.ops] (23.3.5.3.4)) seems to require this operator to be a free function.

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