不能用对类型键设置映射值
Not able set map value with pair type key
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
class NodePoint {
public:
int x, y;
NodePoint (int i, int j){x=i; y=j;}
NodePoint (){}
bool operator == (const NodePoint &rhs) { return (x == rhs.x && y == rhs.y); };
bool operator != (const NodePoint &rhs) { return (x != rhs.x || y != rhs.y); };
};
class Link {
public:
Link (){}
};
int main() {
vector < vector <NodePoint> > nodes;
map < pair < NodePoint, NodePoint > , Link > node_links;
int sol_row_size = 10, sol_col_size = 10, i, j;
for(i=0; i<sol_row_size; i++) {
vector < NodePoint > tn;
for(j=0; j<sol_col_size; j++) tn.push_back(NodePoint(i,j));
nodes.push_back(tn);
}
for(i=0; i<sol_row_size; i++) {
for(j=0; j<(sol_col_size-1); j++) {
node_links[make_pair(nodes[i][j], nodes[i][j+1])] = Link();
}
}
return 0;
}
在上面的程序中,我只是试图创建一个名为node_links
的映射,其中密钥将是一对2个输入节点,但我得到了大量的编译错误(并且在我的代码中没有点一些行),我不太习惯cpp,有什么帮助吗?我做错了什么?
下面的编译错误:
$ g++ temp.cpp -o temp;
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/vector:61,
from temp.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In function ‘bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = NodePoint, _T2 = NodePoint]’:
/usr/include/c++/4.6/bits/stl_function.h:236:22: instantiated from ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<NodePoint, NodePoint>]’
/usr/include/c++/4.6/bits/stl_map.h:452:2: instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<NodePoint, NodePoint>, _Tp = Link, _Compare = std::less<std::pair<NodePoint, NodePoint> >, _Alloc = std::allocator<std::pair<const std::pair<NodePoint, NodePoint>, Link> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Link, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<NodePoint, NodePoint>]’
temp.cpp:35:55: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: no match for ‘operator<’ in ‘__x.std::pair<NodePoint, NodePoint>::second < __y.std::pair<NodePoint, NodePoint>::second’
/usr/include/c++/4.6/bits/stl_pair.h:209:62: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: no match for ‘operator<’ in ‘__y.std::pair<NodePoint, NodePoint>::first < __x.std::pair<NodePoint, NodePoint>::first’
/usr/include/c++/4.6/bits/stl_pair.h:209:62: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: no match for ‘operator<’ in ‘__x.std::pair<NodePoint, NodePoint>::first < __y.std::pair<NodePoint, NodePoint>::first’
/usr/include/c++/4.6/bits/stl_pair.h:209:62: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:207:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:291:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:341:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/stl_vector.h:1290:5: note: template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:866:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator<(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:899:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:817:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator<(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
更新1 我在NodePOint类
中添加了以下内容bool operator < (const NodePoint &rhs) {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
};
,我得到以下错误:
In file included from /usr/include/c++/4.6/bits/stl_algobase.h:65:0,
from /usr/include/c++/4.6/vector:61,
from temp.cpp:1:
/usr/include/c++/4.6/bits/stl_pair.h: In function ‘bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) [with _T1 = NodePoint, _T2 = NodePoint]’:
/usr/include/c++/4.6/bits/stl_function.h:236:22: instantiated from ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::pair<NodePoint, NodePoint>]’
/usr/include/c++/4.6/bits/stl_map.h:452:2: instantiated from ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<NodePoint, NodePoint>, _Tp = Link, _Compare = std::less<std::pair<NodePoint, NodePoint> >, _Alloc = std::allocator<std::pair<const std::pair<NodePoint, NodePoint>, Link> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Link, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<NodePoint, NodePoint>]’
temp.cpp:37:55: instantiated from here
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: passing ‘const NodePoint’ as ‘this’ argument of ‘bool NodePoint::operator<(const NodePoint&)’ discards qualifiers [-fpermissive]
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: passing ‘const NodePoint’ as ‘this’ argument of ‘bool NodePoint::operator<(const NodePoint&)’ discards qualifiers [-fpermissive]
/usr/include/c++/4.6/bits/stl_pair.h:209:62: error: passing ‘const NodePoint’ as ‘this’ argument of ‘bool NodePoint::operator<(const NodePoint&)’ discards qualifiers [-fpermissive]
您需要在NodePoint
上定义operator<
,以便您的节点对可以排序(std::pair<NodePoint, NodePoint>
根据NodePoint::operator<
自动为您定义operator<
)
假设你想要这样的:
bool operator <(const NodePoint &rhs) const {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
};
EDIT添加const
为了使用std::map
,第三个模板参数默认为std::less<KeyType>
,因此您的类必须提供operator<
或其自己的比较器。
您需要定义<</p> 为您映射键的操作符
-
成员-在您的情况下不可能
-
全局(或命名空间范围),
typepedef std::pair <NodePoint,> NodePointPair;
bool operator<(const nodepointair&, const nodepointair&){…}
3。-作为一个函子-然后是映射中的第三个参数与您的函子的名称。阅读练习
相关文章:
- 正在将无序映射设置为无序映射的值
- 从文件读取到映射中,该映射将键作为整数,并将相应的值设置为设置
- C++从 Java 角度设置和映射内存管理
- C++映射/设置迭代器不可递增错误
- 如何将字符设置为函数?我对语法感到困惑...设置<char>max_freq(映射<字符,整数>频率)
- C XTREE崩溃在出口上,映射/设置迭代器不可用
- 我如何将输入分配给在 for 循环中设置的映射功能
- 映射和设置总是一次分配一个项目吗
- 带有内存比较的 STL 映射.如何在运行时设置比较块大小
- c++映射/设置迭代器不可取消引用
- 映射/设置迭代器在 std::map 中不可递增
- 用于设置相关类属性的映射的合理键类型
- 映射/设置迭代器未解除可绑定运行时错误
- 调试断言失败映射/设置迭代器不可取消引用
- 使用提升复制将结构的成员作为键映射到设置容器中
- 映射/设置迭代器不可递增
- 设置/映射迭代器不可递增
- C++映射/设置迭代器在使用多映射时出现不可增加的错误
- 不能用对类型键设置映射值
- 在C++中设置映射*的键和值