0
点赞
收藏
分享

微信扫一扫

C++set集合如何插入结构体struct【no match for ‘operator<‘(operand types are ‘const Node‘ and‘const Node‘)】

云朵里的佛光 2022-03-23 阅读 26
容器c++
#include <bits/stdc++.h>
using namespace std;
struct Node{
	int x,y;
	Node(){}
	Node(int _x,int _y):x(_x),y(_y){}
};
vector<Node> v;
map<int,Node> m;
int main()
{
	v.push_back(Node(1,2));
	m[0]=Node(1,2);
	cout<<v[0].x<<" "<<v[0].y<<endl;
	cout<<m[0].x<<" "<<m[0].y<<endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct Node{
	int x,y;
	Node(){}
	Node(int _x,int _y):x(_x),y(_y){}
	bool operator<(const Node &n)const//const一定要写
	{
		return x<n.x;//根据自己需要来写
	}
};
set<Node> s;
int main()
{
	s.insert(Node(1,2));
	cout<<s.size()<<endl;
	return 0;
}
举报

相关推荐

0 条评论