- 博客(22)
- 资源 (1)
- 收藏
- 关注
原创 第14章-重载运算符与类型转换
重载运算符与类型转换1 基本概念当运算符作用于内置类型的运算对象时,我们无法改变该运算符的含义(不能为内置对象运算符重新定义)。只能重载已有的运算符,而无权发明新的运算符。对于一个重载的运算符来说,优先级和结合律与对应的内置运算符保持一致。不能被重载的运算符有:作用域运算符(::),.*运算符,.运算符和?:运算符。data1+data2;与operator+(data1,
2014-12-21 15:19:42
513
原创 第13章-拷贝控制
拷贝控制拷贝控制操作:拷贝构造函数、拷贝赋值运算符、移动构造函数、移动赋值运算符和析构函数。1 拷贝、赋值和销毁1-1 拷贝构造函数拷贝构造函数在几种情况下都会被隐式地使用,所以,拷贝构造函数通常不应该是explicit的。拷贝构造函数必被用来初始化非引用类类型参数,这一特性解释了为什么拷贝构造函数自己的参数必须是引用类型。如果参数不是引用类型,那么调用
2014-12-18 16:06:40
696
原创 第11章-关联容器
关联容器关联容器类型map关联数组:保存关键字-值对set关键字即值,即只保存关键字的容器multimap关键字可以重复出现的mapmultiset关键字可以重复出现的setunordered_map用哈希函数组织的mapunordered_set用哈希函数组织的setunord
2014-12-17 14:32:47
473
原创 第十章-泛型算法
泛型算法1 概述迭代器令算法不依赖于容器,但是算法依赖于元素类型的操作。泛型算法运行于迭代器之上而不会执行容器操作的特性带来了一个编程假设:算法永远不会改变底层容器的大小。2 初始泛型算法除了少数例外,标准库算法都对一个范围内的元素进行操作,称这个范围为“输入范围”。接受输入范围的算法总是使用前两个参数表示范围,两个参数分别是指向要处理的第一个元
2014-12-16 14:30:43
501
原创 第九章-顺序容器
顺序容器1 顺序容器概述顺序容器类型vector可变大小数组。支持快速随机访问。在尾部之外插入/删除元素可能很慢deque双端队列。支持快速随机访问。在头尾位置插入/删除很快list双向链表。只支持双向顺序访问。在list中任何位置插入/删除操作速度都很快forward_list单向链表。只支持单向顺序访问。在
2014-12-15 15:26:55
456
原创 第八章-IO库
IO库1 IO类为了支持使用宽字符的语言,标准库定义了一组类型和对象来操纵wchar_t类型的数据。宽字符版本的类型和函数的名字以一个w开始。1-1 IO对象无拷贝或赋值由于不能拷贝IO对象,因此也不能将形参或返回类型设置为流类型。进行IO操作的函数通常以引用的方式传递和返回流。读写一个IO对象会改变其状态,因此传递和返回的引用不能是const的。1
2014-12-13 10:34:58
426
原创 第七章-类
类1 定义抽象数据类型类的接口包括用户所能执行的操作;类的实现则包括类的数据成员、负责接口实现的函数体以及类所需的各种私有函数。1-1 设计Sales_data类1-2 定义改进的Sales_data类当我们调用成员函数时,实际上是在替某个对象调用它。成员函数通过一个名为this的额外隐式参数来访问调用它的对象。当我们调用一个成员函数时,用请求该对象
2014-12-12 10:42:22
429
原创 第六章-函数
函数 1 函数基础我们通过调用运算符来执行函数。调用运算符的形式是(),它作用于一个表达式,该表达式是函数或者指向函数的指针。调用函数表达式的类型就是函数的返回类型。 尽管实参与形参存在对应关系,但是并没有规定实参的求值顺序。1-1 局部对象形参和函数体内部定义的变量统称为局部变量。1-2 函数声明如果一个函数永远也不会被我们用到,那么它可以只声明没有定义。
2014-12-11 21:59:33
445
原创 算法导论-第23章-最小生成树:Prime算法(基于vector)的C++实现
#include #include #include #include using namespace std;static char elements_index{ 'a' };using P = pair;struct Vertex { char index{ elements_index++ }; int key{ numeric_limits::max() }; b
2014-12-09 15:30:28
487
原创 算法导论-第23章-最小生成树:Kruskal算法(基于按秩合并、路径压缩的不相交集合)C++实现
#include #include #include using namespace std;static char elements_index{ 'a' };using P = pair;using PP = pair;struct Element { char index{ elements_index++ }; int rank{ 0 }; Element* par
2014-12-09 11:42:53
805
原创 算法导论-第22章-基本的图算法:强连通分量(深度优先遍历基础上)C++实现
#include using namespace std;struct Vertex { int index{ -1 }; Vertex* next{ nullptr };};enum class COLOR { WHITE, GRAY, BLACK};struct Node { int d{}; int f{}; COLOR color{ COLOR::WHITE
2014-12-06 15:05:49
689
原创 算法导论-第32章-字符串匹配:Rabin-Karp算法C++实现
#include #include #include using namespace std;int char_to_int(char c) { return c - '0';}int module(int x, int q) { if (x < 0) { return x % q + q; } else { return x % q; }}bool is_
2014-11-29 20:59:20
828
原创 算法导论-第32章-字符串匹配:有限自动机方法(改进版本:预处理阶段复杂度为O(m*| ∑ |) )C++实现
#include #include #include using namespace std;void built_pai(char P[], int pai[], size_t m) { int k{}; pai[0] = 0; for (int q = 1; q < m; ++q) { while (k > 0 && P[k] != P[q]) { k = pai[k
2014-11-29 19:36:43
617
原创 算法导论-第32章-字符串匹配:Knuth-Morris-Pratt(KMP)算法C++实现
#include #include #include using namespace std;void built_pai(char P[], int pai[], int m) { int k{}; pai[0] = 0; for (int q = 1; q < m; ++q) { while (k > 0 && P[k] != P[q]) { k = pai[k];
2014-11-29 16:10:44
445
原创 算法导论-第15章-动态规划:最长公共子序列(空间复杂度改进版)C++实现
#include #include #include using namespace std;int LCS(char* A, char* B, int* C, int m, int n) { for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { if (A[i] == B[j]) { C[i *
2014-11-27 16:47:28
991
原创 算法导论-第15章-动态规划:矩阵链乘法C++实现
#include #include #include #include using namespace std;long matrix_chain(long P[], long M[], int S[], size_t size) { int l{}; int j{}; int i{}; int k{}; for (l = 2; l <= size; ++l) { for
2014-11-26 21:59:34
458
原创 算法导论-第15章-动态规划:钢条切割问题自底向下方法C++实现
#include #include #include using namespace std;int cut_rod(int P[], int R[], int M[], int size) { int i{}; int j{}; for (i = 1; i < size; ++i) { int max{ -1 }; int max_index{ -1 }; for (
2014-11-26 19:58:06
621
原创 算法导论-第12章-二叉搜索树:随机二叉搜索树数据结构C++实现(前中后序遍历,插入,搜索,前后毗邻元素,最大最小值)
#include #include #include #include using namespace std;struct Node { int key{}; Node* left_son{ nullptr }; Node* right_son{ nullptr }; Node* parent{ nullptr };};class BinarySearchTree {
2014-11-25 20:58:13
557
原创 算法导论-第九章-中位数和顺序统计量:最坏情况为线性时间的选择算法C++实现
#include #include #include #include #include using namespace std;int find_mid(int M[], int size = 5) { vector v{}; for (int i = 0; i < size; ++i) v.push_back(M[i]); sort(v.begin(), v.end()
2014-11-22 20:44:17
891
原创 算法导论-第七章-快速排序:随机化快速排序C++实现
#include #include #include #include #include using namespace std;int partition(int A[], int begin, int end) { int pivot_value {A[end]}; int i {begin - 1}; int j {begin}; for(;j <= end - 1; ++j) { if(A
2014-11-19 21:26:55
578
原创 算法导论-第六章-堆排序:基于最大堆的排序C++实现
#include #include #include using namespace std;class Heap {private: int length; int heap_size; int* A;public: Heap(vector); ~Heap() { delete []A; } int parent(int i) { r
2014-11-18 22:16:15
507
原创 算法导论-第四章-分治策略:最大子数组C++实现
#include #include #include #include #include using namespace std;array find_max_cross_subarray(int A[], int low, int mid, int high) { int left_sum = numeric_limits::min(); int max_left
2014-11-16 20:04:14
506
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人