结构体内嵌比较函数bool operator < (const node &x) const {}

关于结构体内嵌比较函数:

一般情况下:

struct node
{
    int l,r;
    bool operator <(const node &a)const{
        return r < a.r;
    }
}a[maxn];

直接写比较函数是裸的r表示当前的值,如果r<a.r,那么就是从小到大排序,但是优先队列的是相反的。

sort默认为从小到大排序,优先队列默认为从大到小。

struct node
{
    int l,r;
    bool operator <(const node &a)const
    {
        return r>a.r;
    }
};
priority_queue<node> q;

那么这个优先队列是按r小的优先出队。

结构体内嵌比较函数的使用就是直接sort就可以,sort(a,a+n);

当然也可以直接写一个比较的函数:

 bool cmp(node a,node b){
2 
3   return a.r<b.r;
4 
5 }

用法就是sort(a,a+n,cmp);

但是这种排序的方法比结构体内嵌比较函数的慢很多,有的时候超时可能就是排序写挫了。

----------------------------------------------------------------我叫分割线-------------------------------------------------------

以上只是按照一个变量排序的,如果我两个变量都考虑,那怎么排序的呢?

#include<bits/stdc++.h>
using namespace std;
struct node{
    int l,r;

    bool operator < (const node &x) const {
        if(l<=r&&x.l> x.r) return true;
        if(l> r&&x.l< x.r) return false;
        if(l<=r&&x.l<=x.r) return l<x.l;
        if(l> r&&x.l> x.r) return r>x.r;
        //return false;
    }
}a[100];

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i].l,&a[i].r);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)
        cout<<a[i].l<<" "<<a[i].r<<endl;
}

就用上面的代码举栗子吧。

假设我要比较的数列为:

(l,r): (1,3) (4,3) (5,6) (8,2) (7,1) 那么我按照以上代码排序,排完序之后的数列是什么样的呢?

  (1,3) (5,6) (4,3) (8,2) (7,1) 

发现什么了吗?为什么要用两种颜色标记呢?蓝色的都是l<=r的,粉色的都是l>r的,排序的时候,没有参数的l,r比有参数的x.l,x.r的级别高,就相当于是比较的时候的第一个数。

bool operator < (const node &x) const {
        if(l<=r&&x.l> x.r) return true;
        if(l> r&&x.l< x.r) return false;
        if(l<=r&&x.l<=x.r) return l<x.l;
        if(l> r&&x.l> x.r) return r>x.r;
        //return false;
    }

再看一下代码:当我比较的两个数,一个是l<=r的,另一个是l>r的,按照没有参数的为主,看返回的是true还是false。

如果为if(l<=r&&x.l> x.r) return true;

   if(l> r&&x.l<=x.r) return false;    那么l<=r的数都在l>r的数的前面。

所以返回true还是false的作用是把数分成两类,看l<=r的在前面还是l>r的在前面,

if(l<=r&&x.l> x.r) return true;

if(x.l> x.r&&l< r)   return true;

是一样的,这个的顺序并不影响排序的结果。

但是如果我写的是if(l<=r&&x.l> x.r) return false;

        if(l> r&&x.l<=x.r) return true;

那么我返回的就是l>r的在l<=r的数的前面。

然后继续,对于相同的l<=r或者l>r的数而言,我就根据返回的是按照l的大小比较还是r的大小比较就看我返回的是什么。

按上面的例子,if(l<=r&&x.l<=x.r) return l<x.l;

       if(l>  r&&x.l>  x.r) return r>x.r;

那么对于l<=r的数而言,谁的l更小谁在前面,对于l>r的而言,谁的r更大谁在前面。

而且if(l<=r&&&x.l<=x.r) return l<x.l;

   if(l<[r&&x.l<=x.r) return x.l>l;

是一样的,看没有参数的l是大于还是小于有参数的。

  • 17
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
自己实现的字符串类 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
C++的STL中实现由一个bitset类模板,其用法如下: std::bitset bs; 也就是说,这个bs只能支持64位以内的位存储和操作;bs一旦定义就不能动态增长了。本资源附件中实现了一个动态Bitset,和标准bitset兼容。 /** @defgroup Bitset Bitset位集类 * @{ */ //根据std::bitset改写,函数意义和std::bitset保持一致 class CORE_API Bitset: public Serializable { typedef typename uint32_t _Ty; static const int _Bitsperword = (CHAR_BIT * sizeof(_Ty)); _Ty * _Array; //最低位放在[0]位置,每位的默认值为0 int _Bits;//最大有效的Bit个数 private: int calculateWords()const; void tidy(_Ty _Wordval = 0); void trim(); _Ty getWord(size_t _Wpos)const; public: //默认构造 Bitset(); //传入最大的位数,每位默认是0 Bitset(int nBits); virtual ~Bitset(); //直接整数转化成二进制,赋值给Bitset,最高低放在[0]位置 Bitset(unsigned long long _Val); //拷贝构造函数 Bitset(const Bitset & b); Bitset(const char * str); Bitset(const std::string & str, size_t _Pos, size_t _Count); public: size_t size()const; //返回设置为1的位数 size_t count() const; bool subscript(size_t _Pos) const; bool get(size_t pos) const; //设置指定位置为0或1,true表示1,false表示0,如果pos大于数组长度,则自动扩展 void set(size_t _Pos, bool _Val = true); //将位数组转换成整数,最低位放在[0]位置 //例如数组中存放的1011,则返回13,而不是返回11 unsigned long long to_ullong() const; bool test(size_t _Pos) const; bool any() const; bool none() const; bool all() const; std::string to_string() const; public: //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (const Bitset& b); //直接整数转化成二进制,赋值给Bitset,最高位放在[0]位置 Bitset& operator = (unsigned long long ull); //返回指定位置的值,如果pos大于位数组长度,自动拓展 bool operator [] (const size_t pos); //测试两个Bitset是否相等 bool operator == (const Bitset & b); bool operator != (const Bitset & b); Bitset operator<>(size_t _Pos) const; bool operator > (const Bitset & c)const; bool operator < (const Bitset & c)const; Bitset& operator &=(const Bitset& _Right); Bitset& operator|=(const Bitset& _Right); Bitset& operator^=(const Bitset& _Right); Bitset& operator<>=(size_t _Pos); public: Bitset& flip(size_t _Pos); Bitset& flip(); //将高位与低位互相,如数组存放的是1011,则本函数执行后为1101 Bitset& reverse(); //返回左边n位,构成新的Bitset Bitset left(size_t n) const; //返回右边n位,构成新的Bitset Bitset right(size_t n) const; //判断b包含的位数组是否是本类的位数组的自串,如果是返回开始位置 size_t find (const Bitset & b) const; size_t find(unsigned long long & b) const; size_t find(const char * b) const; size_t find(const std::string & b) const; //判断本类的位数组是否是b的前缀 bool is_prefix(unsigned long long & b) const; bool is_prefix(const char * b) const; bool is_prefix(const std::string & b) const; bool is_prefix(const Bitset & b) const; void clear(); void resize(size_t newSize); void reset(const unsigned char * flags, size_t s); void reset(unsigned long long _Val); void reset(const char * _Str); void reset(const std::string & _Str, size_t _Pos, size_t _Count); //左移动n位,返回新的Bitset //extendBits=false "1101" 左移动2位 "0100"; //extendBits=true "1101" 左移动2位 "110100"; Bitset leftShift(size_t n,bool extendBits=false)const; //右移动n位,返回新的Bitset //extendBits=false "1101" 右移动2位 "0011"; //extendBits=true "1101" 右移动2位 "001101"; Bitset rightShift(size_t n, bool extendBits = false) const; public: virtual uint32_t getByteArraySize(); // returns the size of the required byte array. virtual void loadFromByteArray(const unsigned char * data); // load this object using the byte array. virtual void storeToByteArray(unsigned char ** data, uint32_t& length) ; // store this object in the byte array. };

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值