std :: sort和std :: unique结构的唯一问题 [英] std::sort and std::unique problem with a struct

查看:210
本文介绍了std :: sort和std :: unique结构的唯一问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

  #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()
);
}

不会编译出许多错误,例如:


错误C2784:'bool std :: operator
==(const std :: vector< _Ty,_Alloc>&,const std :: vector< ; _Ty,_Alloc>&)':
无法推导
的模板参数const std :: vector< _Ty,_Alloc>&'from
'myStructDim'


我明白我必须重写一个或两个运算符。





感谢您的支持!

解决方案比较运算符来表达小于和平等的关系。定义具有两个参数的独立布尔函数运算符<运算符== ,每个 const myStructDim& ,并按照你所需的方式执行比较,可能比在 struct 中定义then作为方法更简单。


The following code:

#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()
        );
}

will not compile with many errors, such as:

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'

I understand that I have to override an operator or two.

Which ones and how exactly please?

Thanks for the support!

解决方案

You need comparison operators to express the "less-than" and "equality" relationships. Defining stand-alone boolean functions operator< and operator== that take two arguments, each const myStructDim&, and perform the comparison exactly the way you require, is probably simpler than defining then as methods within the struct.

这篇关于std :: sort和std :: unique结构的唯一问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆