#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坐标小的在前 } else return 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坐标小的在前 } else return 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<rc3 CCell 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; }