结构的 std::sort 和 std::unique 问题

发布于 2024-09-17 20:03:46 字数 736 浏览 1 评论 0原文

下面的代码:

#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>; &'从 'myStructDim'

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

请问是哪些以及具体如何?

感谢您的支持!

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!

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

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

发布评论

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

评论(4

尹雨沫 2024-09-24 20:03:46

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

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.

陈甜 2024-09-24 20:03:46

就像其他人提到的运算符<和运算符 == 可以解决问题,但我通常更喜欢传递比较谓词。

我在本例中使用 C++0x lambda,但没有它也可以实现。

   std::sort(
      vec_dim.begin(), 
      vec_dim.end(), 
      [] (myStructDim const & l, myStructDim const & r) {return l.nId < r.nId;}
      ); 

   vec_dim.erase( 
      std::unique(
         vec_dim.begin(), 
         vec_dim.end(),
         [] (myStructDim const & l, myStructDim const & r) {return l.nId == r.nId;}
         ), 
      vec_dim.end() 
      ); 

Like others mentioned operator< and operator== would do the trick but I usually prefer to pass a comparision predicate.

I use C++0x lambdas in this example but it can be implemented without that.

   std::sort(
      vec_dim.begin(), 
      vec_dim.end(), 
      [] (myStructDim const & l, myStructDim const & r) {return l.nId < r.nId;}
      ); 

   vec_dim.erase( 
      std::unique(
         vec_dim.begin(), 
         vec_dim.end(),
         [] (myStructDim const & l, myStructDim const & r) {return l.nId == r.nId;}
         ), 
      vec_dim.end() 
      ); 
说谎友 2024-09-24 20:03:46

您需要某种形式的比较函数来进行排序,并且需要某种形式的相等函数来进行唯一。

You need some form of comparison function for sort, and you need some form of equality function for unique.

孤寂小茶 2024-09-24 20:03:46

如果没有operato>,是否不可能拥有某种独特性? ?我的意思是我可以理解,对于 unique 来说,我需要一个运算符==(就像苹果不是椅子一样),但是为什么椅子应该比苹果更大???我必须为某些没有意义的对象实现一个运算符!也许某种聚类会更有意义。
所以我决定为我自己实现这个问题,在我看来,这是一个更有意义的解决方案:

模板内联
void uniques(listtype In,listtype& Out)
{
Out.resize(In.size());
std::copy(In.begin(),In.end(),Out.begin());
listtype::iterator it = Out.begin();
listtype::iterator it2= Out.begin();
它2++;
int tmpsize = Out.size();

    while(it!=Out.end())
    {
    it2 = it;
    it2++;
    while((it2)!=Out.end())
        {
        if ((*it)==(*it2))
            Out.erase(it2++);
        else
            ++it2;
        }
    it++;

    }
}

也许不是最好的解决方案,但目前我不知道更好的解决方案

Is it not possible to have some kind of unique without having the operato> ? I mean I can understand that for unique I need an operator== (like apples are not chairs) but why should a chair be greater than an apple ??? I would have to implement an operator for some objects where it makes no sense ! maybe some kind of clusering would make more sense.
So I decided to implement what the question is for my self here is in my opinion a solution that makes more sense:

template inline
void uniques(listtype In,listtype& Out)
{
Out.resize(In.size());
std::copy(In.begin(),In.end(),Out.begin());
listtype::iterator it = Out.begin();
listtype::iterator it2= Out.begin();
it2++;
int tmpsize = Out.size();

    while(it!=Out.end())
    {
    it2 = it;
    it2++;
    while((it2)!=Out.end())
        {
        if ((*it)==(*it2))
            Out.erase(it2++);
        else
            ++it2;
        }
    it++;

    }
}

maybe not the best solution but at the moment I don t know betters

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