Sorting objects in STL list

Source: Internet
Author: User
# Pragma once # include <windows. h> class ccell {public: rect; public: ccell (); ccell (rect RC );~ Ccell (void); int getleft () const; // obtain the X coordinate int gettop () const in the upper left corner; // obtain the Y coordinate in the upper left corner };

#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: defines the entry point of the console application. // # 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 (); // front of the rectangle X coordinate smaller} elsereturn cell1.gettop () <cell2.gettop (); // front of the rectangle y coordinate smaller} class CMP Are {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; // front of the rectangle X coordinate smaller} elsereturn rc1.top <rc2.top; // before the Y coordinate of the rectangle} int _ tmain (INT argc, _ tchar * argv []) {rect RC1 = {1, 2, 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 Cell 2 (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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.