C++简单语法错误,了解的来
#include<iostream>
#include<map>
using namespace std;
struct node
{
int x,y;
node(int a,int b):x(a),y(b){}
};
int main()
{
map<char,node>mp;
char ch='1';
node a(ch,2);
mp.insert(make_pair(ch,a));
//cout<<mp[ch].x<<endl;这句为什么错
return 0;
}
[解决办法]
给node写个默认构造函数,因为map在分配自己的结点的时候会直接new出来, 调用到了node的默认构造函数, 之后使用了赋值运算完成了pair到结点内node的赋值。
可以给node写个拷贝构造函数,围观一下这个过程。
[解决办法]
楼主,其实这并不是语法错误,这是map里的机制问题,map之所以能快速的搜索是因为有一个很好的排序机制,二叉树排序,那么排序要用到什么呢?就是>< 符号了啊,但是由于是你自定义的结构体,所以就找不到这个啦 这个你要自己写一个
bool operator<( const node& other,const node& other1 ){ //写你的判断机制}