如何在c++集中插入值

How to insert values in a c++ set

本文关键字:插入 集中 c++      更新时间:2023-10-16

我正在尝试创建一组Point 类的自定义对象

点.h

#ifndef POINT_H
#define POINT_H
class Point
{
      float x_coordinate,y_coordinate;
      public:
             Point(float x, float y):x_coordinate(x),y_coordinate(y)
             {}
             float get_x()
             {
                   return x_coordinate;
             }
             float get_y()
             {
                   return y_coordinate;
             }
             bool operator==(Point rhs)
             {
                  if( ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y()) )
                      return true;
                  else return false;
             }
             bool operator<(Point rhs)
             {
                  if((int)x_coordinate < (int)rhs.get_x())
                      return true;
                  else return false;
             }
};
#endif

我刚开始写驱动程序

#include<iostream>
#include<set>
#include "point.h"
using namespace std;

int main()
{
    Point p1(-10,-10),p2(-10,10),p3(10,10),p4(10,-10);
    set<Point> points_set = set<Point>();
    points_set.insert(p1);
    return 0;
}

但我在编译过程中遇到了这个错误,我已经重载了比较和等式运算符,我还必须做什么才能使它工作?

In file included from /usr/include/c++/4.6/string:50:0,
                 from /usr/include/c++/4.6/bits/locale_classes.h:42,
                 from /usr/include/c++/4.6/bits/ios_base.h:43,
                 from /usr/include/c++/4.6/ios:43,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from driver.cpp:1:
/usr/include/c++/4.6/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Point]’:
/usr/include/c++/4.6/bits/stl_tree.h:1267:4:   instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Point, _Val = Point, _KeyOfValue = std::_Identity<Point>, _Compare = std::less<Point>, _Alloc = std::allocator<Point>]’
/usr/include/c++/4.6/bits/stl_set.h:410:29:   instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const value_type&) [with _Key = Point, _Compare = std::less<Point>, _Alloc = std::allocator<Point>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Point>, std::set<_Key, _Compare, _Alloc>::value_type = Point]’
driver.cpp:12:25:   instantiated from here
/usr/include/c++/4.6/bits/stl_function.h:236:22: error: passing ‘const Point’ as ‘this’ argument of ‘bool Point::operator<(Point)’ discards qualifiers [-fpermissive]
float get_x() const { ... }
float get_y() const { ... }
bool operator==(Point const& rhs) const { ... }
bool operator<(Point const& rhs) const { ... }

在参数和方法本身中,您缺少了所有这些const

注意:我不确定operator<用于set中的项目的要求是什么,但您提供的要求非常弱。

@Mat让我来回答你的具体问题。因此,我只想指出您不习惯使用C++的一些问题。返回布尔值不需要条件。只需返回if语句的值:

bool operator==(const Point& rhs) const
{
    return ((int)x_coordinate == (int)rhs.get_x()) && ((int)y_coordinate == (int)rhs.get_y());
}
bool operator<(const Point& rhs) const
{
    return (int)x_coordinate < (int)rhs.get_x();
}

此外,最好在C++中声明显式强制转换机制,而不是使用C风格的情况:

    return static_cast<int>(x_coordinate) < static_cast<int>(rhs.get_x());

您需要声明运算符==和运算符<作为const成员函数。这将允许在const对象上调用它们,就像存储在一个集合中的所有对象一样:

bool operator<(Point rhs) const

正如其他人所指出的,您的代码示例还有其他问题:

  • get_x和get_y也应该是const方法
  • 您对operator<相比之下过于宽松。std::set在执行插入和查找时使用了"等价"的概念。几个不同的对象可能与运算符<(即它不是"弱排序函数"(肯定会与其他std::set操作发生冲突。特别是,在您的代码示例中,集合无法区分(-10,-10(和(-10,10(。您至少应该比较x和y坐标的点,并使用浮点比较而不是整数比较

正确的签名是:

bool operator<(const Point & rhs) const

此外,您还需要声明const函数get_xget_y

operator=具有类似的签名,但在这种情况下它不是强制性的。

其他人已经讨论了const问题。我想补充一点,当你申报points_set时,你可以简单地说:

set<Point> points_set;

而不是你现在拥有的:

set<Point> points_set = set<Point>();

您需要将Point作为const&两个操作员。运算符本身实际上也应该是const。

我还注意到运算符<不是在比较y坐标,是故意的吗?考虑(0/7(<(0/5(将是假的,并且(0/1(<(0/7(也将为false。