快速排序和C++11新特性Lambda

1.五大排序算法之 快速排序
2. #include
#include
#include
using namespace std;
void fastSort(int intarr[], int start,int end)
{
if (start >= end)
{
return;
}
int starti = start;
int endi = end;
int key = intarr[starti];
while (starti < endi)
{
while (starti < endi && intarr[endi]>=key)//必须是等于,要不然容易死循环
{
endi–;
}
intarr[starti] = intarr[endi];
while (starti < endi && intarr[starti]<=key)//必须是等于,要不然容易死循环
{
starti++;
}
intarr[endi] = intarr[starti];
}
intarr[starti]= key;
fastSort(intarr, start, starti - 1);
fastSort(intarr, starti + 1, end);
}
int main()
{
const int num = 20;
int intarr[num];
srand((unsigned)time(NULL));
for (int i = 0; i < num; i++)
{
int val = rand() % 100;
intarr[i] = val;
cout << val << " ";
}
cout << endl;
fastSort(intarr, 0, num - 1);
for (auto x : intarr)
{
cout << x << " ";
}
cout << endl;
system(“pause”);
return 0;
}
Lambda特性
#include
#include
#include
#include
#include
using namespace std;
int main()
{
auto f = {cout << “Hello world” << endl; };
f();
vector vec;
vec.push_back(5);
vec.push_back(10);
for_each(vec.begin(), vec.end(), [](int val)
{
cout << val << endl;
});
int a = 5;
int b = 10;
auto ff = =->int {return a + b; };
cout <<ff() << endl;
auto f3 = [](int x, int y)->bool {return x > y; };//[]为空的时候,不捕捉外部变量。
cout << f3(4, 5) << endl;
system(“pause”);
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值