Cpp11_1

 1 #include <iostream>   //std::cout
 2 #include <functional> //std::less
 3 #include <algorithm>  //std::sort,std::includes
 4 using namespace std;
 5 int main(int argc, char *argv[])
 6 {
 7     //auto a;//错误, auto 是通过初始化表达式进⾏行类型推导,如果没有初始化表达式,就⽆无法确定a的类型
 8     auto b = 1;
 9     auto c = 1.0;
10     auto d = "Hello Baby";
11     if(typeid(b) == typeid(c))
12     {
13         cout<<"True
";
14     }else
15     {
16         cout<<"False
";
17     }
18     auto func = less<int>();
19 
20     //less
21     int foo[] = {10,20,5,15,25};
22     int bar[] = {15,10,20};
23     std::sort(foo,foo+5,std::less<int>());
24     std::sort(bar,bar+3,std::less<int>());
25     if(std::includes(foo,foo+5,bar,bar+3,std::less<int>()))
26     {//contains 
27         std::cout<< sizeof(foo)/sizeof(int)<<"foo includes bar.
";
28         for(int i=0;i< sizeof(foo)/sizeof(int);i++)
29         {
30             std::cout<<foo[i]<<"
";
31         }
32 
33     }
34 
35     return 0;
36 }

以上,less<int>(),

template <class T> struct less {
  bool operator() (const T& x, const T& y) const {return x<y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef bool result_type;
};

sort:

当你需要按照某种特定方式进行排序时,你需要给sort指定比较函数,否则程序会自动提供给你一个比较函数。

vector < int > vect;
//...
sort(vect.begin(), vect.end());
//此时相当于调用
sort(vect.begin(), vect.end(), less<int>() );

 上述例子中系统自己为sort提供了less仿函数。在STL中还提供了其他仿函数,以下是仿函数列表:

 

名称功能描述
equal_to 相等
not_equal_to 不相等
less 小于
greater 大于
less_equal 小于等于
greater_equal 大于等于

需要注意的是,这些函数不是都能适用于你的sort算法,如何选择,决定于你的应用。另外,不能直接写入仿函数的名字,而是要写其重载的()函数:

less<int>()
greater<int>()

当你的容器中元素时一些标准类型(int float char)或者string时,你可以直接使用这些函数模板。但如果你时自己定义的类型或者你需要按照其他方式排序,你可以有两种方法来达到效果:一种是自己写比较函数。另一种是重载类型的'<'操作赋。

#include <iostream>   //std::cout
#include <functional> //std::less
#include <algorithm> //std::sort,std::includes
#include <vector>
using namespace std;

class myclass
{
public:
myclass(int a,int b):first(a),second(b)
{
//cout<<"a:"<<a<<" ";
//cout<<"b:"<<b<<" ";
//cout<<"first:"<<first<<" ";
//cout<<"second:"<<second<<" ";
}
int first;
int second;
bool operator < (const myclass &m)const
{
//cout<<"operator ";
cout<<"first:"<<first<<endl;
cout<<"m.first:"<<m.first<<endl;
cout<<"second:"<<second<<endl;
cout<<"m.second:"<<m.second<<endl;
return second < m.second;
}
};
bool less_second(const myclass & m1,const myclass & m2)
{
return m1.second < m2.second;
}

int main() {
vector<myclass> vect;
for (int i = 0; i < 10; i++) {
myclass my(10 - i, i * 3);
vect.push_back(my);
}
for (int j = 0; j < vect.size(); j++)
{
//cout<<"vect:"<<vect[j].first<<" "<<vect[j].second<<endl;
}
for(int i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<") ";
sort(vect.begin(), vect.end());//调用myClass 重载的 <
cout<<"after sorted by first:"<<endl;
for(int i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<") ";
cout<<"after sorted by second:"<<endl;
sort(vect.begin(), vect.end(), less_second);
for(int i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<") ";

return 0 ;
}

输出结果:

(10,0)
(9,3)
(8,6)
(7,9)
(6,12)
(5,15)
(4,18)
(3,21)
(2,24)
(1,27)
first:9
m.first:10
second:3
m.second:0
first:9
m.first:10
second:3
m.second:0
first:8
m.first:10
second:6
m.second:0
first:8
m.first:9
second:6
m.second:3
first:7
m.first:10
second:9
m.second:0
first:7
m.first:8
second:9
m.second:6
first:6
m.first:10
second:12
m.second:0
first:6
m.first:7
second:12
m.second:9
first:5
m.first:10
second:15
m.second:0
first:5
m.first:6
second:15
m.second:12
first:4
m.first:10
second:18
m.second:0
first:4
m.first:5
second:18
m.second:15
first:3
m.first:10
second:21
m.second:0
first:3
m.first:4
second:21
m.second:18
first:2
m.first:10
second:24
m.second:0
first:2
m.first:3
second:24
m.second:21
first:1
m.first:10
second:27
m.second:0
first:1
m.first:2
second:27
m.second:24
after sorted by first:
(10,0)
(9,3)
(8,6)
(7,9)
(6,12)
(5,15)
(4,18)
(3,21)
(2,24)
(1,27)
after sorted by second:
(10,0)
(9,3)
(8,6)
(7,9)
(6,12)
(5,15)
(4,18)
(3,21)
(2,24)
(1,27)

另:

sort与stable_sort、partion与stable_partion

其中的区别是,带有stable的函数可保证相等元素的原本相对次序在排序后保持不变。或许你会问,既然相等,你还管他相对位置呢,也分不清楚谁是谁了?这里需要弄清楚一个问题,这里的相等,是指你提供的函数表示两个元素相等,并不一定是一摸一样的元素。

参考

http://www.cplusplus.com/reference/functional/less/

http://developer.51cto.com/art/201312/422379.htm

http://cpprocks.com/

http://towriting.com/blog/2013/08/01/what-is-cpp11/

http://blog.csdn.net/bz201/article/details/543001  在读中

原文地址:https://www.cnblogs.com/CGAlpha/p/6901735.html