The STL implementation version of the specified structure sort field (C + + 2011 series)

Source: Internet
Author: User

C2011.cpp: Defines the entry point for a console application.
//

#include "stdafx.h"
#include <vector>
#include <map>
#include <regex>
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
#include <sstream>
using namespace Std;

Class record_t
{
Private
Public
int m_groupid;
Double M_fee;
float M_price;
Std::string M_name;
Public
record_t (): M_name (""), M_fee (0), m_groupid (0), M_price (0)
{
//
}
record_t (const std::string &name,double fee,int groupid,float Price)
: M_name (name), M_fee (fee), M_groupid (GroupID), M_price (Price)
{
//
}

Operator operator << overloads, Friend declaration supports access to record_t private variables
Friend Inline ostream &operator << (ostream &os,const record_t)
{
os<< "m_fee->" <<r.m_fee<< "# #m_groupid->"
<<r.m_groupid<< "# #m_name->" <<r.m_name
<< "M_price:" <<r.m_price<<endl;
return OS;
}

Const std::string GetName () const
{
return m_name;
}

Double Getfee () const
{
return m_fee;
}

int Getgroupid () const
{
return m_groupid;
}

void Destroy ()
{
Delete this;
}
};

Template<typename t,typename r> class Compare_t:public std::unary_function<t,r>
{
Private
Comparison rules
int m_rule;
Public
compare_t (int rule): m_rule (rule)
{
//
}

Support t& Comparisons
R operator () (T &t1,t &t2)
//{
return compare (T1,T2);
//}

Support for const t& comparisons
R operator () (const T &t1,const t &t2)
{
return compare (T1,T2);
}

Support for t* pointer comparisons
R operator () (T *t1,t *t2)
//{
return compare (*T1,*T2);
//}

Supports const t* pointer comparisons
R operator () (const T *t1,const t *t2)
{
return compare (*T1,*T2);
}

 //Implementation comparison rules
 r compare (const T &t1,const t &t2)
 {
  r result = false;
& Nbsp; switch (m_rule)
  {
  case 1:
   {
     result = T1.getname () < T2.getname () True:false;
    break;
   }
  case 2:
   {
    result = T1.getfee () < T2.getfee ()? True:false;
    break;
   }
  default:
   {
    result = (T1.getgroupid () > T2.getgroupid ()) True:false;
    break;
   }
  } 
  return result;
 
};

int main (int argc, char* argv[])
{
 std::array<std::string,10> aa; 
 std::fill_n ( Aa.begin (), Aa.size (), "AA");
 std::string total = "";
 //lambda expression [&parameter1,&parameter2,...] (parameterlist)
 std::for_each (Aa.begin (), Aa.end (), [&total] (std::string &i) { 
  total + = ( Total.empty ()? "": "#") + I;
 });
 std::cout<<total<<endl;
 

Pre-allocating memory
const unsigned int SIZE = 10;
Std::vector<record_t*> RR (SIZE);
for (size_t i = 0,j = Size;i < size;++i,--j)
{
Rr[i] = new record_t ("AA", J,i, (float) i);
}

Specifying comparison rules
int compare_rule = 1;
Compare_t<record_t,bool> comp = compare_t<record_t,bool> (compare_rule);
Std::sort (Rr.begin (), Rr.end (), comp);
Shorthand
Std::sort (Rr.begin (), Rr.end (),compare_t<record_t,bool> (Compare_rule));
for (size_t i = 0;i < Size;++i)
{
std::cout<< (*rr[i]) <<endl;
}

Std::vector<record_t> RRR (SIZE);
Std::sort (Rrr.begin (), Rrr.end (),compare_t<record_t,bool> (Compare_rule));

Std::vector<const record_t> rrrr (SIZE);
Std::sort (Rrrr.begin (), Rrrr.end (),compare_t<record_t,bool> (Compare_rule));

Std::vector<const record_t*> rrrrr (SIZE);
Std::copy (Rr.begin (), Rr.end (), Rrrrr.begin ());
Std::sort (Rrrrr.begin (), Rrrrr.end (),compare_t<record_t,bool> (Compare_rule));

destructor, freeing memory
Std::for_each (Rr.begin (), Rr.end (), Std::mem_fun (&record_t::d Estroy));
Rr.clear ();

System ("pause");

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.