c++ - std::sort 和 std::unique 结构问题

标签 c++ stl operators compilation std

以下代码:

#include <vector>
#include <algorithm>

struct myStructDim
{
    int     nId;
    int     dwHeight;
    int     dwWidth;
};    

void main()
{
    ::std::vector<myStructDim>  m_vec_dim;

    ::std::sort(m_vec_dim.begin(), m_vec_dim.end());
    m_vec_dim.erase(
        ::std::unique(m_vec_dim.begin(), m_vec_dim.end()),
        m_vec_dim.end()
        );
}

不会编译,有很多错误,例如:

error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'myStructDim'

我知道我必须覆盖一两个运算符。

请问具体是哪些?

感谢支持!

最佳答案

您需要比较运算符来表达“小于”和“等于”关系。定义独立的 bool 函数 operator<operator==有两个参数,每个参数 const myStructDim& , 并完全按照您需要的方式执行比较,可能比将 then 定义为 struct 中的方法更简单。 .

关于c++ - std::sort 和 std::unique 结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640663/

相关文章:

c++ - 快速调用 fread 会使应用程序崩溃

c++ - 当我用 g++ 编译器编译 c++ 时,错误是什么意思?

c++ - 列表指针类的输出 vector

c++ - 如何修复 'invalid operands to binary expression' ?

ruby - => 运算符有什么作用?

c++ - 追求更好的位标志枚举

c++ - 如何使用 C++ 在运行时获取内存使用情况?

c++ - C++ STL 中 max_element 和 minmax_element 的行为差异

python : recursion loop in rich comparison operator

f# - 无法让绑定(bind)运算符与可区分联合一起使用