如何在c++集中插入值
How to insert values in a c++ set
我正在尝试创建一组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_x
和get_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。
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 如何从存储在std::映射中的std::集中删除元素
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 如何在 c++ 集中插入元素,直到按下 Enter 键
- 使用插入C 后,将订单保持在无序集中
- 如何有效地将一系列连续整数插入std::集中
- 如何在c++集中插入值
- C++:使用 std::function 在多集中插入元组,并保持顺序
- 为什么我不能在 STL 集中插入 600 万个元素?
- HDF5:在数据集中插入一组字符串