c++
析构函数简介
它的作用与构造函数相反,一般是执行对象的清理工作,当对象的生命周期结束的时候,会自动的调用。析构函数的作用并不是删除对象,在对象撤销它所占用的内存之前,做一些清理的工作。清理之后,这部分内存就可以被系统回收再利用了。在设计这个类的时候,系统也会默认的提供一个析构函数。在对象的生命周期结束的时候,程序就会自动执行析构函数来完成这些工作。同构造函数,用户自己定义,系统自动调用。
1.析构函数没有返回值,没有参数;
2.没有参数,所以不能重载,一个类仅有一个析构函数;
3.析构函数除了释放工作,还可以做一些用户希望它做的一些工作,比如输出一些信息。
c++ STL:
容器:
序列式容器:vector,list,array,deque
关联式容器(底层是红黑树):set,map,multiset,multimap,有序的
无序关联式容器(基于hash):unordered_map,unordered_multimap,unordered_set,unordered_multiset
容器适配器:对序列化容器进行封装,stack,queue,priority_queue
迭代器适配器:反向迭代器适配器、插入型迭代器适配器、流迭代器适配器、流缓冲区迭代器适配器、移动迭代器适配器
算法:
1. C++ sort()排序函数用法详解:对容器或普通数组中 [first, last) 范围内的元素进行排序,默认进行升序排序。
- 容器支持的迭代器类型必须为随机访问迭代器。这意味着,sort() 只对 array、vector、deque 这 3 个容器提供支持。
- 如果对容器中指定区域的元素做默认升序排序,则元素类型必须支持
<
小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符; - sort() 函数在实现排序时,需要交换容器中元素的存储位置。这种情况下,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。
- 不是稳定的排序方式
-
#include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector //以普通函数的方式实现自定义排序规则 bool mycomp(int i, int j) { return (i < j); } //以函数对象的方式实现自定义排序规则 class mycomp2 { public: bool operator() (int i, int j) { return (i < j); } }; int main() { std::vector<int> myvector{ 32, 71, 12, 45, 26, 80, 53, 33 }; //调用第一种语法格式,对 32、71、12、45 进行排序 std::sort(myvector.begin(), myvector.begin() + 4); //(12 32 45 71) 26 80 53 33 //调用第二种语法格式,利用STL标准库提供的其它比较规则(比如 greater<T>)进行排序 std::sort(myvector.begin(), myvector.begin() + 4, std::greater<int>()); //(71 45 32 12) 26 80 53 33 //调用第二种语法格式,通过自定义比较规则进行排序 std::sort(myvector.begin(), myvector.end(), mycomp2());//12 26 32 33 45 53 71 80 //输出 myvector 容器中的元素 for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { std::cout << *it << ' '; } return 0; }
2. C++ stable_sort()用法详解:sort()的稳定排序方式
3. C++ partial_sort()函数详解:topN的排序方式
- 容器支持的迭代器类型必须为随机访问迭代器。这意味着,partial_sort() 函数只适用于 array、vector、deque 这 3 个容器。
- 当选用默认的升序排序规则时,容器中存储的元素类型必须支持 <小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符;
- partial_sort() 函数在实现过程中,需要交换某些元素的存储位置。因此,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。
-
#include <iostream> // std::cout #include <algorithm> // std::partial_sort #include <vector> // std::vector using namespace std; //以普通函数的方式自定义排序规则 bool mycomp1(int i, int j) { return (i > j); } //以函数对象的方式自定义排序规则 class mycomp2 { public: bool operator() (int i, int j) { return (i > j); } }; int main() { std::vector<int> myvector{ 3,2,5,4,1,6,9,7}; //以默认的升序排序作为排序规则,将 myvector 中最小的 4 个元素移动到开头位置并排好序 std::partial_sort(myvector.begin(), myvector.begin() + 4, myvector.end()); cout << "第一次排序:\n"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << *it << ' '; cout << "\n第二次排序:\n"; // 以指定的 mycomp2 作为排序规则,将 myvector 中最大的 4 个元素移动到开头位置并排好序 std::partial_sort(myvector.begin(), myvector.begin() + 4, myvector.end(), mycomp2()); for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << *it << ' '; return 0; }
partial_sort_copy() 函数的功能和 partial_sort() 类似,唯一的区别在于,前者不会对原有数据做任何变动,而是先将选定的部分元素拷贝到另外指定的数组或容器中,然后再对这部分元素进行排序。
4. C++ nth_element()用法详解:第n个元素
- 容器支持的迭代器类型必须为随机访问迭代器。这意味着,nth_element() 函数只适用于 array、vector、deque 这 3 个容器。
- 当选用默认的升序排序规则时,容器中存储的元素类型必须支持 <小于运算符;同样,如果选用标准库提供的其它排序规则,元素类型也必须支持该规则底层实现所用的比较运算符;
- nth_element() 函数在实现过程中,需要交换某些元素的存储位置。因此,如果容器中存储的是自定义的类对象,则该类的内部必须提供移动构造函数和移动赋值运算符。
5. C++ is_sorted()函数完全攻略:判断某个序列是否满足定义的排序方式
#include <iostream> // std::cout #include <algorithm> // std::is_sorted #include <vector> // std::array #include <list> // std::list using namespace std; //以普通函数的方式自定义排序规则 bool mycomp1(int i, int j) { return (i > j); } //以函数对象的方式自定义排序规则 class mycomp2 { public: bool operator() (int i, int j) { return (i > j); } }; int main() { vector<int> myvector{ 3,1,2,4 }; list<int> mylist{ 1,2,3,4 }; //调用第 2 种语法格式的 is_sorted() 函数,该判断语句会得到执行 if (!is_sorted(myvector.begin(), myvector.end(),mycomp2())) { cout << "开始对 myvector 容器排序" << endl; //对 myvector 容器做降序排序 sort(myvector.begin(), myvector.end(),mycomp2()); //输出 myvector 容器中的元素 for (auto it = myvector.begin(); it != myvector.end(); ++it) { cout << *it << " "; } } //调用第一种语法格式的 is_sorted() 函数,该判断语句得不到执行 if (!is_sorted(mylist.begin(), mylist.end())) { cout << "开始对 mylist 排序" << endl; //...... } return 0; }
6. C++ STL标准库这么多排序函数,该如何选择?7. 自定义STL算法规则,应优先使用函数对象!
8. C++ merge()和inplace_merge()函数用法(详解版)
C++ merge():两个有序序列合并;inplace_merge():两个有序序列(在同一个容器中)合并
//该数组中存储有 2 个有序序列 int first[] = { 5,10,15,20,25,7,17,27,37,47,57 }; //用于存储新的有序序列 vector<int> myvector(11); //将 [first,first+5) 和 [first+5,first+11) 合并为 1 个有序序列,并存储到 myvector 容器中。 merge(first, first + 5, first + 5, first +11 , myvector.begin()); #include <iostream> // std::cout #include <algorithm> // std::merge using namespace std; int main() { //该数组中存储有 2 个有序序列 int first[] = { 5,10,15,20,25,7,17,27,37,47,57 }; //将 [first,first+5) 和 [first+5,first+11) 合并为 1 个有序序列。 inplace_merge(first, first + 5,first +11); for (int i = 0; i < 11; i++) { cout << first[i] << " "; } return 0; }
9. C++ find()函数用法详解(超级详细):用于在指定范围内查找和目标元素值相等的第一个元素。
#include <iostream> // std::cout #include <algorithm> // std::find #include <vector> // std::vector using namespace std; int main() { //find() 函数作用于普通数组 char stl[] ="http://c.biancheng.net/stl/"; //调用 find() 查找第一个字符 'c' char * p = find(stl, stl + strlen(stl), 'c'); //判断是否查找成功 if (p != stl + strlen(stl)) { cout << p << endl; } //find() 函数作用于容器 std::vector<int> myvector{ 10,20,30,40,50 }; std::vector<int>::iterator it; it = find(myvector.begin(), myvector.end(), 30); if (it != myvector.end()) cout << "查找成功:" << *it; else cout << "查找失败"; return 0; }
10. 能用STL算法,绝不自己实现!11. STL算法和容器中的成员方法同名时,该如何选择?
12. C++ find_if()和find_if_not()函数用法详解:自定义查找规则find第一个满足规则的元素
#include <iostream> // std::cout #include <algorithm> // std::find_if #include <vector> // std::vector using namespace std; //自定义一元谓词函数 bool mycomp(int i) { return ((i % 2) == 1); } //以函数对象的形式定义一个 find_if() 函数的查找规则 class mycomp2 { public: bool operator()(const int& i) { return ((i % 2) == 1); } }; int main() { vector<int> myvector{ 4,2,3,1,5 }; //调用 find_if() 函数,并以 IsOdd() 一元谓词函数作为查找规则 vector<int>::iterator it = find_if(myvector.begin(), myvector.end(), mycomp2()); cout << "*it = " << *it; return 0; } #include <iostream> // std::cout #include <algorithm> // std::find_if_not #include <vector> // std::vector using namespace std; //自定义一元谓词函数 bool mycomp(int i) { return ((i % 2) == 1); } int main() { vector<int> myvector{4,2,3,1,5}; //调用 find_if() 函数,并以 mycomp() 一元谓词函数作为查找规则 vector<int>::iterator it = find_if_not(myvector.begin(), myvector.end(), mycomp); cout << "*it = " << *it; return 0; }
13. C++ find_end()函数详解:最后一次出现的位置
#include <iostream> // std::cout #include <algorithm> // std::find_end #include <vector> // std::vector using namespace std; //以普通函数的形式定义一个匹配规则 bool mycomp1(int i, int j) { return (i%j == 0); } //以函数对象的形式定义一个匹配规则 class mycomp2 { public: bool operator()(const int& i, const int& j) { return (i%j == 0); } }; int main() { vector<int> myvector{ 1,2,3,4,8,12,18,1,2,3 }; int myarr[] = { 1,2,3 }; //调用第一种语法格式 vector<int>::iterator it = find_end(myvector.begin(), myvector.end(), myarr, myarr + 3); if (it != myvector.end()) { cout << "最后一个{1,2,3}的起始位置为:" << it - myvector.begin() << ",*it = " << *it << endl; } int myarr2[] = { 2,4,6 }; //调用第二种语法格式 it = find_end(myvector.begin(), myvector.end(), myarr2, myarr2 + 3, mycomp2()); if (it != myvector.end()) { cout << "最后一个{2,3,4}的起始位置为:" << it - myvector.begin() << ",*it = " << *it; } return 0; }
14. C++ find_first_of()函数完全攻略:A 序列中查找和 B 序列中任意元素相匹配的第一个元素
#include <iostream> // std::cout #include <algorithm> // std::find_first_of #include <vector> // std::vector using namespace std; //自定义二元谓词函数,作为 find_first_of() 函数的匹配规则 bool mycomp(int c1, int c2) { return (c2 % c1 == 0); } //以函数对象的形式定义一个 find_first_of() 函数的匹配规则 class mycomp2 { public: bool operator()(const int& c1, const int& c2) { return (c2 % c1 == 0); } }; int main() { char url[] = "http://c.biancheng.net/stl/"; char ch[] = "stl"; //调用第一种语法格式,找到 url 中和 "stl" 任一字符相同的第一个字符 char *it = find_first_of(url, url + 27, ch, ch + 4); if (it != url + 27) { cout << "*it = " << *it << '\n'; } vector<int> myvector{ 5,7,3,9 }; int inter[] = { 4,6,8 }; //调用第二种语法格式,找到 myvector 容器中和 3、5、7 任一元素有 c2%c1=0 关系的第一个元素 vector<int>::iterator iter = find_first_of(myvector.begin(), myvector.end(), inter, inter + 3, mycomp2()); if (iter != myvector.end()) { cout << "*iter = " << *iter; } return 0; }
15. C++ adjacent_find()函数用法详解:用于在指定范围内查找 2 个连续相等的元素
#include <iostream> // std::cout #include <algorithm> // std::search #include <vector> // std::vector using namespace std; //以普通函数的形式定义一个匹配规则 bool mycomp1(int i, int j) { return (i%j == 0); } //以函数对象的形式定义一个匹配规则 class mycomp2 { public: bool operator()(const int& i, const int& j) { return (i%j == 0); } }; int main() { vector<int> myvector{ 1,2,3,4,8,12,18,1,2,3 }; int myarr[] = { 1,2,3 }; //调用第一种语法格式 vector<int>::iterator it = search(myvector.begin(), myvector.end(), myarr, myarr + 3); if (it != myvector.end()) { cout << "第一个{1,2,3}的起始位置为:" << it - myvector.begin() << ",*it = " << *it << endl; } int myarr2[] = { 2,4,6 }; //调用第二种语法格式 it = search(myvector.begin(), myvector.end(), myarr2, myarr2 + 3, mycomp2()); if (it != myvector.end()) { cout << "第一个{2,3,4}的起始位置为:" << it - myvector.begin() << ",*it = " << *it; } return 0; }
18. C++ partition()和stable_partition()函数详解:partition() 函数可根据用户自定义的筛选规则,重新排列指定区域内存储的数据,使其分为 2 组,第一组为符合筛选条件的数据,另一组为不符合筛选条件的数据。
#include <iostream> // std::cout #include <algorithm> // std::partition #include <vector> // std::vector using namespace std; //以普通函数的方式定义partition()函数的筛选规则 bool mycomp(int i) { return (i % 2) == 0; } //以函数对象的形式定义筛选规则 class mycomp2 { public: bool operator()(const int& i) { return (i%2 == 0); } }; int main() { std::vector<int> myvector{1,2,3,4,5,6,7,8,9}; std::vector<int>::iterator bound; //以 mycomp2 规则,对 myvector 容器中的数据进行分组 bound = std::partition(myvector.begin(), myvector.end(), mycomp2()); for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) { cout << *it << " "; } cout << "\nbound = " << *bound; return 0; }
partition() 函数只负责对指定区域内的数据进行分组,并不保证各组中元素的相对位置不发生改变。而如果想在分组的同时保证不改变各组中元素的相对位置,可以使用 stable_partition() 函数。
19. C++ partition_copy()函数详解:不改变原有容器元素
20. C++ partition_point()函数(详解版)
21. C++ lower_bound()函数用法详解:lower_bound() 函数用于在指定区域内查找不小于目标值的第一个元素。
#include <iostream> // std::cout #include <algorithm> // std::lower_bound #include <vector> // std::vector using namespace std; //以普通函数的方式定义查找规则 bool mycomp(int i,int j) { return i>j; } //以函数对象的形式定义查找规则 class mycomp2 { public: bool operator()(const int& i, const int& j) { return i>j; } }; int main() { int a[5] = { 1,2,3,4,5 }; //从 a 数组中找到第一个不小于 3 的元素 int *p = lower_bound(a, a + 5, 3); cout << "*p = " << *p << endl; vector<int> myvector{ 4,5,3,1,2 }; //根据 mycomp2 规则,从 myvector 容器中找到第一个违背 mycomp2 规则的元素 vector<int>::iterator iter = lower_bound(myvector.begin(), myvector.end(),3,mycomp2()); cout << "*iter = " << *iter; return 0; }
22. C++ upper_bound()函数(精讲版):用于在指定范围内查找大于目标值的第一个元素
#include <iostream> // std::cout #include <algorithm> // std::upper_bound #include <vector> // std::vector using namespace std; //以普通函数的方式定义查找规则 bool mycomp(int i, int j) { return i > j; } //以函数对象的形式定义查找规则 class mycomp2 { public: bool operator()(const int& i, const int& j) { return i > j; } }; int main() { int a[5] = { 1,2,3,4,5 }; //从 a 数组中找到第一个大于 3 的元素 int *p = upper_bound(a, a + 5, 3); cout << "*p = " << *p << endl; vector<int> myvector{ 4,5,3,1,2 }; //根据 mycomp2 规则,从 myvector 容器中找到第一个违背 mycomp2 规则的元素 vector<int>::iterator iter = upper_bound(myvector.begin(), myvector.end(), 3, mycomp2()); cout << "*iter = " << *iter; return 0; }
23. C++ equel_range()函数详解:用于在指定范围内查找等于目标值的所有元素
#include <iostream> // std::cout #include <algorithm> // std::equal_range #include <vector> // std::vector using namespace std; //以普通函数的方式定义查找规则 bool mycomp(int i, int j) { return i > j; } //以函数对象的形式定义查找规则 class mycomp2 { public: bool operator()(const int& i, const int& j) { return i > j; } }; int main() { int a[9] = { 1,2,3,4,4,4,5,6,7}; //从 a 数组中找到所有的元素 4 pair<int*, int*> range = equal_range(a, a + 9, 4); cout << "a[9]:"; for (int *p = range.first; p < range.second; ++p) { cout << *p << " "; } vector<int>myvector{ 7,8,5,4,3,3,3,3,2,1 }; pair<vector<int>::iterator, vector<int>::iterator> range2; //在 myvector 容器中找到所有的元素 3 range2 = equal_range(myvector.begin(), myvector.end(), 3,mycomp2()); cout << "\nmyvector:"; for (auto it = range2.first; it != range2.second; ++it) { cout << *it << " "; } return 0; }
24. C++ binary_search()函数详解25. C++(STL) all_of、any_of及none_of算法详解26. C++ equal(STL equal)比较算法详解27. C++ mismatch(STL mismatch)算法详解28. C++(STL) lexicographical_compare字符串排序算法详解29. C++ next_permutation(STL next_permutation)算法详解30. C++ prev_permutation(STL prev_permutation)算法详解31. C++ is_permutation(STL is_permutation)算法详解32. C++ copy_n(STL copy_n)算法详解33. C++ copy_if(STL copy_if)算法详解34. C++ copy_backward(STL copy_backward)算法详解35. C++ reverse_copy(STL reverse_copy)算法详解36. C++ unique(STL unique)算法详解37. C++ rotate(STL rotate)算法详解38. C++ rotate_copy(STL rotate_copy)算法详解39. C++ move(STL move)函数使用详解40. C++ swap_ranges(STL swap_ranges)函数使用详解41. C++ remove、remove_copy、remove_if和remove_copy_if函数使用详解42. C++ fill和fill_n函数用法详解43. C++(STL)generate和generate_n函数用法详解44. C++ transform(STL transform)函数用法详解45. C++ replace,replace_if和replace_copy函数用法详解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程