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

本文探讨了函数对象的概念及如何通过重载operator()来实现更灵活的调用方式,并详细解析了一个矩阵类的设计实现,包括多种构造函数、索引运算符重载以及矩阵尺寸调整方法。

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

1、

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

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


//泛型findMax,带有函数对象   
//前提:a.size()>0

template
const 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()));
	}
};

template
const 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,输出默认顺序 <
template
const Object& findMax(const vector& arr)
{
	return findMax2(arr, less{});//less输出默认顺序 <
}

int main()
{
	//输出结果为  crododile  ZEBRA  crododile
	vector 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()

//矩阵
template
class 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[]重载,我的理解就是注释部分了,感觉作者还是很体谅我这种人的,写出来让我注意到我原来并不知道为什么,哈哈~

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值