您的位置:首页 > 编程语言 > Go语言

usage of algorithm

2016-07-06 14:21 399 查看
# include <iostream>
# include <cstdlib>
# include <vector>
# include <algorithm>      //  for_each()

using namespace std;

// 回调函数
void current(int& v)
{
cout << v << endl;
}

void print(vector<int> vec)
{
cout << "The Elements are : " << endl;
for_each(vec.begin(), vec.end(), current); // 遍历算法 遍历打印 current 回调函数
}

int compare(int& a, int& b)
{
return a < b; // 从小到达 如果 A < B就可以
}

int main()
{
vector<int> v(10);

for(int i=9; i>=0; i--)
{
v[9-i] = i;
}

print(v);

sort(v.begin(), v.end(), compare);     // 排序的算法 compare 回调函数

print(v);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息