#pragma once#include <Windows.h>class CCell{public:RECT rect;public:CCell();CCell(RECT rc);~CCell(void);int GetLeft() const;//擷取左上方x座標int GetTop() const;//擷取左上方y座標};
#include "StdAfx.h"#include "Cell.h"CCell::CCell(){}CCell::CCell(RECT rc){rect = rc;}CCell::~CCell(void){}int CCell::GetLeft() const{return rect.left;}int CCell::GetTop() const{return rect.top;}
// sort.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include "Cell.h"#include <list>#include <iostream>using namespace std;class Cmpare1 { public: bool operator()(const CCell cell1,const CCell cell2) const; };bool Cmpare1::operator()(const CCell cell1,const CCell cell2) const{if (cell1.GetTop() == cell2.GetTop()){return cell1.GetLeft() < cell2.GetLeft();//矩形x座標小的在前}elsereturn cell1.GetTop() < cell2.GetTop();//矩形y座標小的在前}class Cmpare { public: bool operator()(const RECT rc1,const RECT rc2) const; };bool Cmpare::operator()(const RECT rc1,const RECT rc2) const{if (rc1.top == rc2.top){return rc1.left < rc2.left;//矩形x座標小的在前}elsereturn rc1.top < rc2.top;//矩形y座標小的在前}int _tmain(int argc, _TCHAR* argv[]){RECT rc1 = {1,2,3,4};RECT rc2 = {2,1,3,4};RECT rc3 = {3,2,3,4};RECT rc4 = {2,2,3,4};//rc2<rc1<rc4<rc3CCell cell1(rc1);CCell cell2(rc2);CCell cell3(rc3);CCell cell4(rc4);//std::list<RECT> rlist;//rlist.push_back(rc1);//rlist.push_back(rc2);//rlist.push_back(rc3);//rlist.push_back(rc4);//rlist.sort(Cmpare());std::list<CCell> clist;clist.push_back(cell1);clist.push_back(cell2);clist.push_back(cell3);clist.push_back(cell4);clist.sort(Cmpare1());//std::list<RECT>::iterator iter;//for (iter = rlist.begin();iter != rlist.end();iter++)//{//cout<<iter->left<<iter->top<<iter->right<<iter->bottom<<endl;//}std::list<CCell>::iterator iter;for (iter = clist.begin();iter != clist.end();iter++){cout<<iter->rect.left<<iter->rect.top<<iter->rect.right<<iter->rect.bottom<<endl;}return 0;}