# STL常用排序算法介绍

456人阅读 评论(0)

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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：371153次
• 积分：7691
• 等级：
• 排名：第3126名
• 原创：400篇
• 转载：30篇
• 译文：11篇
• 评论：82条
博客专栏
 iOS Developer Library 文章：12篇 阅读：19640
阅读排行