堆栈的重载比较运算符
Overloading comparison operators for stacks
我在这里需要一些帮助:我被要求对 2 个堆栈的比较运算符进行一些重载。 我已经弄清楚了语法,我只是在编写定义时遇到麻烦。所以请帮助我。
至少一个操作员过载,然后我会为其余的做。
struct linklist
{
int no;
struct linklist *next;
};
class Stack
{
private:
linklist *list,*head;
public://constructor and destructor
Stack();
~Stack();
public:// main functions
void push();
void show();
void pop();
public://overloaded operations
friend bool operator == (const Stack &stack1, const Stack &stack2);
friend bool operator != (const Stack &stack1, const Stack &stack2);
friend bool operator < (const Stack &stack1, const Stack &stack2);
friend bool operator > (const Stack &stack1, const Stack &stack2);
};
这实际上取决于您实际想要比较的内容。是堆栈的标识还是堆栈上的元素数量?由于您要定义更小和更大的运算符,因此我假设您要比较堆栈中的元素数量。
相等运算符如下所示:
bool operator==( const Stack &stack1, const Stack &stack2)
{
return stack1.list->no == stack2.list->no;
}
当然,您需要考虑 Stack 对象的列表成员为 NULL 的情况。
相关文章:
- 为什么比较运算符如此快速
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 关于 std::min, std::max 中的比较运算符的混淆
- 三向比较运算符成员与非成员实现
- 标准::可选枚举的比较运算符
- 为什么字符串比较的 == 运算符相对于任一字符串长度线性时间(似乎)?
- C++分数混合比较运算符错误
- <<运算符优先级与字符串比较
- 不能将重载比较运算符与 Catch 测试一起使用
- 重构使用动态强制转换的 std::set 的比较运算符
- ">"运算符和"<"运算符如何进行字符串比较?
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- C++使用关系运算符比较字符串
- 为什么使用 and 运算符比较 if 语句中的 2 个对象会抛出错误,而使用 2 if 语句则不会
- 使用关系运算符比较指针意味着什么
- 运算符<比较多个字段
- 运算符==比较两个不同的类
- 使用==运算符比较字符串到0
- 使用'if'语句和"<<"时的结果错误,'>>'运算符比较 3 个整数