- 论坛徽章:
- 0
|
在使用set 容器保存数据, 出现的问题,
set容器的使用有什么要求呢?
我自定义了一个类 MatrixNode 并实现其中的比较操作, 但使用set插入数据时报错
提示好像是set中的二叉树中比较操作的问题,
以下是代码。
MatrixNode.h
- // 该类以一维vector数组保存矩阵。并实现比较操作以及copy构造函数
- #ifndef MATRIXNODE_H
- #define MATRIXNODE_H
- #include <vector>;
- class MatrixNode
- {
- public:
- //根据行数,列数初始化数据,
- MatrixNode( unsigned short x, unsigned short y );
- MatrixNode();
- ~MatrixNode();
- // 赋值构造函数
- MatrixNode& operator=(MatrixNode &matrix);
-
- bool operator==(const MatrixNode &matrix);
-
- // 比较函数
- bool operator<(const MatrixNode &cmpmatrix);
- MatrixNode(const MatrixNode& tmpmatrix);
- public:
-
- unsigned short m_y;
- unsigned short m_x;
- std::vector<unsigned short>; m_vmatrix; // 保存数据的数组.
- };
- #endif
复制代码
MatrixNode.cpp
main.cpp
- #include <iostream>;
- #include <stdlib.h>;
- #include "matrixnode.h"
- #include <set>;
- using namespace std;
- int main(int argc, char *argv[])
- {
- set<MatrixNode>; a;
- MatrixNode b(3,3);
- a.insert(b); // 插入数据
- return 0;
- }
复制代码
以前没使用过set容器,
请教以下这该如何解决?
报错显示
1042 D:\Program Files\Dev-Cpp\include\c++\3.3.
\bits\stl_tree.h instantiated from `std::pair<std::_Rb_tree_iterator<_Val, _Val&, _Val*>;, bool>; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>;::insert_unique(const _Val& [with _Key = MatrixNode, _Val = MatrixNode, _KeyOfValue = std::_Identity<MatrixNode>;, _Compare = std::less<MatrixNode>;, _Alloc = std::allocator<MatrixNode>;]' |
|