C++/STL-set数组报错

#include "pt4.h"
using namespace std;

class TextView
{
    int x = 0, y = 0;
    int width = 1, height = 1;
public:
    void GetOrigin(int& x, int& y);
    void SetOrigin(int x, int y);
    void GetSize(int& width, int& height);
    void SetSize(int width, int height);
};

void TextView::GetOrigin(int& x, int& y)
{
    x = this->x;
    y = this->y;
}
void TextView::SetOrigin(int x, int y)
{
    this->x = x;
    this->y = y;
}
void TextView::GetSize(int& width, int& height)
{
    width = this->width;
    height = this->height;
}
void TextView::SetSize(int width, int height)
{
    this->width = width;
    this->height = height;
}

class Shape
{
protected:
	int x1; int y1; int x2; int y2;
public:
	Shape(int x1, int y1, int x2, int y2):x1(x1), y1(y1), x2(x2), y2(y2){}
    virtual string GetInfo() = 0;
    virtual void MoveBy(int a, int b) = 0;
    bool operator <(Shape &ob) const 
    {
    	if(x1 == ob.x1)
    	{
    		if(y1 == ob.y1)
    		{
    			if(x2 == ob.x2)
    				return y2 < ob.y2;
    			else return x2 < ob.x2;
			}
			else return y1 < ob.y1;
		}
		else return x1 < ob.x1;
	}
};
class RectShape : public Shape
{
public:
	RectShape(int x1, int y1, int x2, int y2):Shape(x1, y1, x2, y2){}
	
    virtual string GetInfo()
    {
    	return "R(" + to_string(x1) + "," + to_string(y1) + ")(" + to_string(x2) + "," + to_string(y2) + ")";
		//"R(%d,%d)(%d,%d)".format(x1,y1,x2,y2);
	}
    virtual void MoveBy(int a, int b)
    {
    	x1 += a;
    	x2 += a;
    	y1 += b;
    	y2 += b;	
	}
};
class TextShape : public Shape
{
private:
	string tview;
public:
	TextShape(string tview, int x1, int y1, int x2, int y2):Shape(x1, y1, x2, y2){
		this->tview = tview;
	}
    virtual string GetInfo()
    {
    	return "T(" + to_string(x1) + "," + to_string(y1) + ")(" + to_string(x2) + "," + to_string(y2) + ")";
	}
    virtual void MoveBy(int a, int b)
    {
    	x1 += a;
    	x2 += a;
    	y1 += b;
    	y2 += b;
	}
};
void Solve()
{
    Task("OOP2Struc3");
	int N, A, B;
	pt >> N;
	vector<vector<int>> V;
	int num;
	for(int i=0; i<N; i++)
	{
		vector<int> v;
		for(int j=0; j<5; j++) 
		{
			pt >> num;
			v.push_back(num);
		}
		V.push_back(v);
	}
	pt >> A >> B;
	set<Shape> s;
	for(int i=0; i<N; i++)
	{
		if((char)V[i][0] == 'R')
			s.insert(RectShape(V[i][1], V[i][2], V[i][3], V[i][4]));
		else if((char)V[i][0] == 'T')
			s.insert(TextShape("T", V[i][1], V[i][2], V[i][3], V[i][4]));
	}
	for(auto it = s.begin(); it != s.end(); it++)
	{
		it->MoveBy(A, B);
		it->GetInfo();
	}		 
}

这个错误是因为set的迭代器it有const修饰符,不能对其元素进行更改。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值