STL常用排序算法介绍
merge()
以下是排序和通用算法:提供元素排序策略
merge: 合并两个有序序列,存放到另一个序列。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
void printV(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
}
void play_merge()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
vector<int> v2;
v2.push_back(2);
v2.push_back(4);
v2.push_back(6);
vector<int> v3;
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
printV(v3);
// 1 2 3 4 5 6
}
int main()
{
play_merge();
return 0;
}
sort()
sort: 以默认升序的方式重新排列指定范围内的元素。若要改排序规则,可以输入比较函数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
Student(string name, int id) : name(name), id(id) {}
friend bool CompareStudent(Student &s1, Student &s2);
string name;
int id;
};
bool CompareStudent(Student &s1, Student &s2)
{
return s1.id < s2.id;
}
void play_sort()
{
Student s1("lucifer", 1);
Student s2("zhang", 2);
Student s3("yao", 3);
Student s4("qi", 4);
vector<Student> v;
v.push_back(s4);
v.push_back(s2);
v.push_back(s1);
v.push_back(s3);
for (vector<Student>::iterator it = v.begin(); it != v.end(); ++it) {
cout << "name: " << it->name << " id: " << it->id << endl;
}
/*
name: qi id: 4
name: zhang id: 2
name: lucifer id: 1
name: yao id: 3
*/
sort(v.begin(), v.end(), CompareStudent);
for (vector<Student>::iterator it = v.begin(); it != v.end(); ++it) {
cout << "name: " << it->name << " id: " << it->id << endl;
}
/*
name: lucifer id: 1
name: zhang id: 2
name: yao id: 3
name: qi id: 4
*/
}
int main()
{
play_sort();
return 0;
}
random_shuffle()
random_shuffle: 对指定范围内的元素随机调整次序。
srand(time(0)); //设置随机种子
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
void printIntVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
}
void play_random_shuffle()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(7);
v.push_back(9);
printIntVector(v);
// 1 3 5 7 9
srand(time(0));
random_shuffle(v.begin(), v.end());
printIntVector(v);
// 1 5 9 3 7
}
int main()
{
play_random_shuffle();
return 0;
}
reverse()
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
void printIntVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << ' ';
}
cout << endl;
}
void play_reverse()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(7);
v.push_back(9);
printIntVector(v);
// 1 3 5 7 9
reverse(v.begin(), v.end());
printIntVector(v);
// 9 7 5 3 1
}
int main()
{
play_reverse();
return 0;
}
STL常用排序算法介绍的更多相关文章
- C++ STL 常用排序算法
C++ STL 常用排序算法 merge() 以下是排序和通用算法:提供元素排序策略 merge: 合并两个有序序列,存放到另一个序列. 例如: vecIntA,vecIntB,vecIntC是用ve ...
- STL常用查找算法介绍
adjacent_find() 在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器.否则返回past-the-end. #include <io ...
- 常用的排序算法介绍和在JAVA的实现(二)
一.写随笔的原因:本文接上次的常用的排序算法介绍和在JAVA的实现(一) 二.具体的内容: 3.交换排序 交换排序:通过交换元素之间的位置来实现排序. 交换排序又可细分为:冒泡排序,快速排序 (1)冒 ...
- C++进阶 STL(3) 第三天 函数对象适配器、常用遍历算法、常用排序算法、常用算数生成算法、常用集合算法、 distance_逆序遍历_修改容器元素
01昨天课程回顾 02函数对象适配器 函数适配器是用来让一个函数对象表现出另外一种类型的函数对象的特征.因为,许多情况下,我们所持有的函数对象或普通函数的参数个数或是返回值类型并不是我们想要的,这时候 ...
- 转载部长一篇大作:常用排序算法之JavaScript实现
转载部长一篇大作:常用排序算法之JavaScript实现 注:本文是转载实验室同门王部长的大作,找实习找工作在即,本文颇有用处!原文出处:http://www.cnblogs.com/ywang172 ...
- 常用排序算法的python实现和性能分析
常用排序算法的python实现和性能分析 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试 ...
- 面试中常用排序算法实现(Java)
当我们进行数据处理的时候,往往需要对数据进行查找操作,一个有序的数据集往往能够在高效的查找算法下快速得到结果.所以排序的效率就会显的十分重要,本篇我们将着重的介绍几个常见的排序算法,涉及如下内容: 排 ...
- 第四百一十五节,python常用排序算法学习
第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...
- Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法
Java 常用排序算法/程序员必须掌握的 8大排序算法 本文由网络资料整理转载而来,如有问题,欢迎指正! 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排 ...
随机推荐
- jQuery中$(function()与(function($)等的区别详细讲解
(function($) {-})(jQuery); 这里实际上是匿名函数,如下: function(arg){-} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的, ...
- UDP单播和组播使用SO_REUSEADDR 测试结果
UDP单播通信 一. 预置条件 A.B在同一台机器,网络中存在往A.B所在的机器的8888端口发送单播UDP数据 A:端口复用绑定在端口8888上 B:端口复用绑定在端口8888上操作步骤:(1)先启 ...
- logstash分析日志
待处理日志格式如下: [totalCount: 298006556, count: 287347623, queryCount: 259027994, exeCount: 28319629, tota ...
- Ubuntu和ROS一起愉快玩耍
Ubuntu和ROS重要的两个中文网址: Ubuntu:http://cn.ubuntu.com/ROS:http://wiki.ros.org/cn Robots and drones on Ubu ...
- REFRESH删除POSTGRESQL
sudo apt-get install python-psycopg2sudo apt-get install postgresql sudo su - postgres createuser -- ...
- Android 5.0新控件——TextInputLayout
Android 5.0(M)新控件--TextInputLayout 介绍之前,先直观的看一下效果 TextInputLayout其实是一个容器,他继承自LinearLayout,该容器是作用于Tex ...
- Spark-1.6.0中的Sort Based Shuffle源码解读
从Spark-1.2.0开始,Spark的Shuffle由Hash Based Shuffle升级成了Sort Based Shuffle.即Spark.shuffle.manager从Hash换成了 ...
- Centos7下Redis3.2的安装配置与JReid测试
环境 Centos7 Redis版本 3.2.0 安装目录 /usr/local/redis/redis-3.2.0 Redis的介绍 参见官网 安装 1 安装gcc与tcl # yum instal ...
- matlab中的sub2ind函数
在matlab中,矩阵的存储是按列优先,sub2ind函数将矩阵中指定元素的行列下标转换成存储的序号,即线性索引号.下面,我们举例子进行说明. 1 建立一个3*4*2的矩阵 rng(0,'twiste ...
- Java基础---Java---IO流-----读取键盘录入、InputStreamReader、转换流、OutputStreamWriter、InputStreamReader
字符流: FileReader FileWriter BufferedReader BufferedWriter 字节流: FileInputStream FileOutputStream Buffe ...