小编sti*_*ma6的帖子

为什么与<运算符的矢量比较比较每个项目两次?

在这个例子中,比较两个向量与<operator result in operator <,在Integer类上定义,为每个元素调用两次.但是,将两个向量与==运算符进行比较时不会发生这种情况.

#include<iostream>
#include<vector>

class Integer {
    public:
        Integer(int value) : m_value(value) {}
        friend bool operator<(const Integer& lhs, const Integer& rhs);
        friend bool operator==(const Integer& lhs, const Integer& rhs);

    private:
        int m_value;

}; 
bool operator<(const Integer& lhs, const Integer& rhs) {
            std::cout << lhs.m_value << " < " << rhs.m_value << '\n';
            return lhs.m_value < rhs.m_value;
}
bool operator==(const Integer& lhs, const Integer& rhs) {
            std::cout << lhs.m_value << " == " << rhs.m_value << '\n';
            return …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading stdvector

9
推荐指数
2
解决办法
263
查看次数

标签 统计

c++ ×1

operator-overloading ×1

stdvector ×1