文章目录
一、排序函数
函数 | 功能 |
---|---|
sort (first, last, comp) | 对容器或普通数组中 [first, last) 范围内的元素进行排序,默认进行升序排序 |
stable_sort (first, last, comp) | 和 sort() 函数功能相似,不同之处在于,对于 [first, last) 范围内值相同的元素,该函数不会改变它们的相对位置。 |
partial_sort (first, middle, last) | 从 [first,last) 范围内,筛选出 muddle-first 个最小的元素并排序存放在 [first,middle) 区间中。 |
is_sorted (first, last) | 检测 [first, last) 范围内是否已经排好序,默认检测是否按升序排序。 |
1. sort() 函数
sort() 函数受到底层实现方式的限制,它仅适用于普通数组和部分类型的容器。只有普通数组和具备以下条件的容器,才能使用 sort() 函数:
- 容器支持的迭代器类型必须为随机访问迭代器。这意味着,sort() 只对
array
、vector
、deque
这 3 个容器提供支持。(均支持[]
随机访问) - 如果对容器中指定区域的元素做默认升序排序,则元素类型必须支持
<
小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符; - sort() 函数在实现排序时,需要交换容器中元素的存储位置。这种情况下,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动
构造函数
和移动赋值运算符
。
sort()函数利用了快速排序,算法复杂度为 N*logN
sort()
函数有 2 种用法,其语法格式分别为:
#include <algorithm> // head file
//对 [first, last) 区域内的元素做默认的升序排序
void sort (RandomAccessIterator first, RandomAccessIterator last);
//按照指定的 comp 排序规则,对 [first, last) 区域内的元素进行排序
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
其中,排序规则默认以元素值的大小做升序排序,即排序规则为 std::less<T>
2. 使用 sort() 函数
以vector 为例,分别进行升序和降序排序:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v{5, 2, 8, 3, 4, 9, 6, 1};
sort(v.begin(), v.begin()+5); // (2 3 4 5 8) 9 6 1
for (auto elem : v){
cout << elem << " ";
}
cout << endl;
sort(v.begin(), v.end(), greater<int>()); // 9 8 6 5 4 3 2 1
for (auto elem : v){
cout << elem << " ";
}
cout << endl;
return 0;
}
结果为:
2 3 4 5 8 9 6 1
9 8 6 5 4 3 2 1
3. 自定义数据类型
下面自定义Student类,根据学号对数据进行排序:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Student{
public:
int number;
string name;
Student(string na, int num) : name(na), number(num) {}
};
int main()
{
vector<Student> v;
v.push_back(Student("Lucy", 2));
v.push_back(Student("Smith", 5));
v.push_back(Student("Tom", 1));
v.push_back(Student("Domy", 4));
v.push_back(Student("Tim", 3));
sort(v.begin(), v.end(), mycmp());
cout << "number" << "\t" << "name" << endl;
for (auto elem : v){
cout << elem.number << "\t" << elem.name << endl;
}
return 0;
}
此时编译运行会报错,原因是没有自定义 <
比较运算符,有两种解决办法:
(1)在类内部 重载小于号运算符
class Student{
public:
int number;
string name;
Student(string na, int num) : name(na), number(num) {}
bool operator < (const Student &s){
return number < s.number;
}
};
(2)在类外部自定义比较大小的函数,放在 struct
里面:
struct mycmp{
bool operator() (const Student &s1, const Student &s2){
return s1.number < s2.number;
}
};
此时输出结果为:
number name
1 Tom
2 Lucy
3 Tim
4 Domy
5 Smith
5. is_sorted() 函数
is_sorted() 函数有 2 种语法格式,分别是:
//判断 [first, last) 区域内的数据是否符合 std::less<T> 排序规则,即是否为升序序列
bool is_sorted (ForwardIterator first, ForwardIterator last);
//判断 [first, last) 区域内的数据是否符合 comp 排序规则
bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
返回值为 bool
类型,用法和 sort
函数完全一致
二、C++ merge()函数
merge() 函数用于将 2 个 有序 序列合并为 1 个有序序列,前提是这 2个有序序列的排序规则相同(要么都是升序,要么都是降序)。并且最终借助该函数获得的新有序序列,其排序规则也和这 2 个有序序列相同。
merge() 函数的语法格式为:
//以默认的升序排序作为排序规则
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result);
//以自定义的 comp 规则作为排序规则
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
- result 为输出迭代器,用于为最终生成的新有序序列指定存储位置;
- comp 用于自定义排序规则
- 该函数会返回一个输出迭代器,其指向的是新有序序列中最后一个元素之后的位置
举个例子:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v1{5, 2, 8, 3, 4, 9, 6, 1};
vector<int> v2{6, 8, 9, 4, 7, 3, 1, 0};
sort(v1.begin(), v1.end(), greater<int>());
sort(v2.begin(), v2.end(), greater<int>());
int length = v1.size() + v2.size();
vector<int> v(length);
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin(), greater<int>());
for (auto elem : v){
cout << elem << " ";
}
return 0;
}
输出结果为:
9 9 8 8 7 6 6 5 4 4 3 3 2 1 1 0
三、find查找函数
1. find() 函数
序列容器(list、deque、vector、array)无自带的 find() 函数,可以使用 库里面的find函数。find函数可以应用到:
- 数组元素查找
- 字符串中的字符查找
- 序列容器元素查找
对于 find() 函数的底层实现,C++ 标准库中给出了参数代码:
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
下面举例分析:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int a[6] = {1, 2, 3, 4, 5, 6};
int* p = find(a, a+6, 5);
if (p != a+6){
cout << "successfully" << endl;
}
string str="abcdefg";
auto char_p = find(str.begin(), str.end(), 'c');
if (char_p != str.end()){
cout << "successfully" << endl;
}
vector<int> v{2, 1, 6, 3, 7, 4, 9, 5, 8};
auto it = find(v.begin(), v.end(),5);
if (it != v.end()){
cout << "succrssfully!" << endl;
}
return 0;
}
输出结果为:
successfully
successfully
succrssfully!
2. find_if() 函数
find_if()
函数会根据指定的查找规则,在指定区域内查找第一个符合该函数要求(使函数返回 true)的元素。
find_if()
函数的语法格式如下:
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
一般用函数对象的形式定义查找规则,如查找2和3的公倍数的规则为:
class mycomp2 {
public:
bool operator()(const int& i) {
return ((i%2 == 0) && (i%3 == 0));
}
};
单独定义一个 bool
函数来作为规则,并将 bool()
作为第三个参数传入 find_if
函数会报错,原因不详 QAQ…
find_if 举例:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v{1, 2, 3, 4, 5, 6, 7};
vector<int>::iterator it = find_if(v.begin(), v.end(), mycomp2());
if (it != v.end()){
cout << *it << endl;
}
return 0;
}
输出结果为:
6
四、replace 函数
replace()
replace() 算法会用新的值来替换和给定值相匹配的元素。参数描述如下:
- 前两个参数是被处理序列的
正向迭代器
- 第 3 个参数是被替换的值
- 第 4 个参数是新的值。
使用举例:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v {1, 2, 3, 4, 5};
replace(v.begin(), v.end(), 4, 0);
for (auto elem : v){
cout << elem << " ";
}
return 0;
}
结果为:
1 2 3 0 5
replace_if()
replace_if() 会将使谓词返回 true 的元素替换为新的值。它的第 3 个参数是一个谓词,第 4 个参数是新的值。参数的类型一般是元素类型的 const 引用;const 不是强制性的,但谓词不应该改变元素。
replace_if 使用与 container
、array
、string
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
bool find_space(const char &ch){
return (ch == ' ');
}
bool find_first_odd(const int &n){
return n % 5 == 0;
}
int main()
{
string str = "Hello word!";
replace_if(str.begin(), str.end(), find_space, '_');
cout << str << endl;
vector<int> v {0, 1, 2, 3, 4};
replace_if(v.begin(), v.end(), find_first_odd, 100);
for (auto elem : v){
cout << elem << " ";
}
return 0;
}
输出结果为:
Hello_word!
100 1 2 3 4
参考内容:C++常用算法(排序、合并、搜索和分区)