STL list中對象排序

來源:互聯網
上載者:User
#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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.