basic C++ knowledge prepared for data structure (2)

来源:互联网 发布:matlab 矩阵存入cell 编辑:程序博客网 时间:2024/06/04 18:58

看完第一章,很多用过的东西看见了别人家的用法,很多不知所以然的东西有了看似恍然大悟却又朦朦胧胧

1、

函数对象 以及巧妙地重载 operator () 来实现一切的调用更像函数调用

函数对象(function object),一种考虑到一个对象可以既包含数据又包含成员函数,那么当定义一个没有数据而只有一个函数的类,然后传递该类的一个实例,就可以实现把函数作为参数传递了,只不过是放入一个对象之内来传递。


//泛型findMax,带有函数对象   //前提:a.size()>0templateconst Object& findMax1(const vector& arr, Comparator cmp){int maxIndex = 0;for (int i = 0; i < arr.size(); ++i) {if (cmp.isLessThan(arr[maxIndex], arr[i]))maxIndex = i;}return arr[maxIndex];}class CaseInsensitiveCompare1{public:bool isLessThan(const string& lhs, const string& rhs) const{return (strcmp(lhs.c_str(), rhs.c_str()));}};templateconst Object& findMax2(const vector& arr, Comparator isLessThan){int maxIndex = 0;for (int i = 0; i < arr.size(); ++i) {if (isLessThan(arr[maxIndex], arr[i]))//更像函数调用maxIndex = i;}return arr[maxIndex];}//把isLessThan 函数改变成 operator()重载class CaseInsensitiveCompare2{public:bool operator()(const string& lhs, const string& rhs)const{return strcmp(lhs.c_str(), rhs.c_str()) > 0;}};#include//为了用标准库函数less,输出默认顺序 const Object& findMax(const vector& arr){return findMax2(arr, less{});//less输出默认顺序 <}int main(){//输出结果为  crododile  ZEBRA  crododilevector arr = { "ZEBRA","alligator","crododile" };cout << findMax1(arr, CaseInsensitiveCompare1{}) << endl;cout << findMax2(arr, CaseInsensitiveCompare2{}) << endl;cout << findMax(arr);getchar();}


使用operator()时,cmp.operator()(x,y)可以简写成cmp(x,y)更像一个函数调用,有木有,其实operator()被称为函数调用操作符,似乎想到了什么...

感觉这种方式很6,记录下来,常看常思
2、
人家实现了一个矩阵 martix,加上后面的习题让补充零参数构造函数和改变矩阵大小的函数resize()

//矩阵templateclass martix{public:    //让添加零参数构造函数,感觉保证总有一个构造函数被调用吧explicit martix() : array(0) {}martix(int rows, int cols) :array(rows){for (auto& thisRow : array)thisRow.resize(cols);}martix(vector> v) :array{v} {}//拷贝构造martix(vector>&& v) :array{move(v)} {}//移动构造//矩阵的[][] 要重载以实现第二个[]//不能是传值,会大量复制//不能只有常引用,那么将不能出现在 = 左边//不能只有引用,由于如有一个const矩阵,没有[]与之对应const vector& operator[](int row)const{return array[row];}vector& operator[](int row){return array[row];}int numrows()const{return array.size();}int numcols()const{return numrows() ? array[0].size() : 0;}// 改变当前矩阵大小,试验显示里面不能偷懒调用构造函数void resize(int rows, int cols){int rs = this->numrows();int cs = this->numcols();if (rows == rs && cols == cs)return;else if (rows == rs && cols != cs){for (auto& thisRow : array)thisRow.resize(cols);}else if (rows != rs && cols == cs){array.resize(rows);for (int i = rs; i < rows; ++i){array[i].resize(cols);}}else{array.resize(rows);for (auto& thisRow : array)thisRow.resize(cols);}}private:vector> array;};
书中特意书写了为什么有两种operator[]重载,我的理解就是注释部分了,感觉作者还是很体谅我这种人的,写出来让我注意到我原来并不知道为什么,哈哈~

看了算法,才感觉涉及人的智商在算法的世界的差距真的是比人与狗的差距都大。




原创粉丝点击