#《Essential C++》读书笔记# 第六章 以template进行编程
练习题答案
练习6.1 试改写以下类,使它成为一个class template:
class example
{
public:
example(double min, double max);
example(const double* array, int size);
double& operator[](int index);
bool operator==(const example&) const;
bool insert(const double*, int);
bool insert(double);
double min() const { return _min; }
double max() const { return _max; }
void min(double);
void max(double);
int count(double value) const; private:
int size;
double* parray;
double _min;
double _max;
};
改写后:
template <typename elemType>
class example
{
public:
example(const elemType& min, const elemType& max);
example(const elemType* array, int size);
elemType& operator[](int index);
bool operator==(const example&) const;
bool insert(const elemType*, int);
bool insert(const elemType&);
elemType min() const { return _min; }
elemType max() const { return _max; }
void min(const elemType&);
void max(const elemType&);
int count(const elemType& value) const; private:
int _size;
elemType* _parray;
elemType _min;
elemType _max;
};
练习6.2 重新以template形式实现练习4.3的Matrix class,并扩充其功能,使它能够通过通过heap memory(堆内存)来支持任意行列大小。分配/释放内存的操作,请在constructor/destructor中进行。
Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std; template <typename elemType>
class Matrix; template <typename elemType>
class Matrix
{
friend Matrix operator+ <elemType> (const Matrix&, const Matrix&);
template <typename elemType>
friend Matrix<elemType> operator* (const Matrix<elemType>&, const Matrix<elemType>&); public:
Matrix(int rows, int columns);
Matrix(const Matrix&);
~Matrix() { delete[] _matrix; }
Matrix& operator=(const Matrix&);
void operator+= (const Matrix&);
elemType& operator()(int row, int column)
{
return _matrix[row * cols() + column];
} const elemType& operator()(int row, int column) const
{
return _matrix[row * cols() + column];
} int rows() const { return _rows; }
int cols() const { return _cols; } bool same_size(const Matrix& m) const
{
return rows() == m.rows() && cols() == m.cols();
} bool comfortable(const Matrix& m) const
{
return (cols() == m.rows());
} ostream& print(ostream&) const; protected:
int _rows;
int _cols;
elemType* _matrix;
}; template <typename elemType>
inline ostream& operator<<(ostream& os, const Matrix<elemType>& m)
{
return m.print(os);
} template<typename elemType>
Matrix<elemType> operator+(const Matrix<elemType>& m1, const Matrix<elemType>& m2)
{
//确定m1和m2大小相同
Matrix<elemType> result(m1);
result += m2;
return result;
} template<typename elemType>
Matrix<elemType> operator * (const Matrix<elemType>& m1, const Matrix<elemType>& m2)
{
//m1的行数(row)必须等于m2的列数(column)
Matrix<elemType> result(m1.rows(), m2.cols());
for (int ix = 0;ix < m1.rows();++ix)
{
for (int jx = 0;jx < m1.cols();++jx)
{
result(ix, jx) = 0;
for (int kx = 0;kx < m1.cols();++kx)
{
result(ix, jx) += m1(ix, kx) * m2(kx, jx);
}
}
}
return result;
} template <typename elemType>
void Matrix<elemType>::operator+=(const Matrix& m)
{
//确定m1和m2的大小相同
int matrix_size = cols() * rows();
for (int ix = 0;ix < matrix_size;++ix)
{
(*(_matrix + ix)) += (*(m._matrix + ix));
}
} template <typename elemType>
ostream& Matrix<elemType> ::print(ostream& os) const
{
int col = cols();
int matrix_size = col * rows();
for (int ix = 0;ix < matrix_size;++ix)
{
if (ix % col == 0)
os << endl;
os << (*(_matrix + ix)) << ' ';
}
os << endl;
return os;
} template <typename elemType>
Matrix<elemType>& Matrix<elemType>::operator=(const Matrix& rhs)
{
if (this != &rhs)
{
_rows = rhs._rows;
_cols = rhs._cols;
int mat_size = _rows * _cols;
delete[] _matrix;
_matrix = new elemType[mat_size];
for (int ix = 0;ix < mat_size;++ix)
_matrix[ix] = rhs._matrix[ix];
}
return *this;
} template <typename elemType>
Matrix<elemType>::Matrix(int rows, int columns) :_rows(rows), _cols(columns)
{
int size = _rows * _cols;
_matrix = new elemType[size];
for (int ix = 0;ix < size;++ix)
_matrix[ix] = elemType();
} template <typename elemType>
Matrix<elemType>::Matrix(const Matrix& rhs)
{
_rows = rhs._rows;
_cols = rhs._cols;
int mat_size = _rows * _cols;
_matrix = new elemType[mat_size];
for (int ix = 0;ix < mat_size;++ix)
_matrix[ix] = rhs._matrix[ix];
} #endif main.cpp #include "Matrix.h"
#include <fstream> int main()
{
ofstream log("log.txt");
if (!log)
{
cerr << "can't open log file!\n";
return 0;
} Matrix<float> identity(4, 4);
log << "identity: " << identity << endl;
float ar[16] = { 1.,0.,0.,0.,0.,1.,0.,0.,
0.,0.,1.,0.,0.,0.,0.,1. }; for (int i = 0, k = 0;i < 4;++i)
{
for (int j = 0;j < 4;++j)
identity(i, j) = ar[k++];
}
log << "identity after set: " << identity << endl; Matrix<float> m(identity);
log << "m: memberwise initialized: " << m << endl; Matrix<float> m2(8, 12);
log << "m2: 8*12: " << m2 << endl;
m2 = m;
log << "m2 after memberwise assigned to m: "
<< m2 << endl; float ar2[16] = { 1.3,0.4,2.6,8.2,6.2,1.7,1.3,8.3,
4.2,7.4,2.7,1.9,6.3,8.1,5.6,6.6 }; Matrix<float> m3(4, 4);
for (int ix = 0, kx = 0;ix < 4;++ix)
for (int j = 0;j < 4;++j)
m3(ix, j) = ar2[kx++]; log << "m3: assigned random values: " << m3 << endl; Matrix<float> m4 = m3 * identity;
log << m4 << endl;
Matrix<float> m5 = m3 + m4;
log << m5 << endl; m3 += m4;
log << m3 << endl; return 0;
}
感谢https://www.cnblogs.com/lv-anchoret/p/8342842.html、https://blog.csdn.net/mind_v/article/details/70228402 给出的友元重载解决方法。
end。
“巅峰诞生虚伪的拥趸,黄昏见证虔诚的信徒。”
#《Essential C++》读书笔记# 第六章 以template进行编程的更多相关文章
- 《Microsoft Sql server 2008 Internals》读书笔记--第六章Indexes:Internals and Management(1)
<Microsoft Sql server 2008 Internals>索引文件夹: <Microsoft Sql server 2008 Internals>读书笔记--文 ...
- C primer plus 读书笔记第六章和第七章
这两章的标题是C控制语句:循环以及C控制语句:分支和跳转.之所以一起讲,是因为这两章内容都是讲控制语句. 第六章的第一段示例代码 /* summing.c --对用户输入的整数求和 */ #inclu ...
- #《Essential C++》读书笔记# 第四章 基于对象的编程风格
基础知识 Class的定义由两部分组成:class的声明,以及紧接在声明之后的主体.主体部分由一对大括号括住,并以分号结尾.主体内的两个关键字public和private,用来标示每个块的" ...
- C++ primer plus读书笔记——第7章 函数——C++的编程模块
第7章 函数--C++的编程模块 1. 函数的返回类型不能是数组,但可以是其他任何一种类型,甚至可以是结构和对象.有趣的是,C++函数不能直接返回数组,但可以将数组作为结构或对象的组成部分来返回. 2 ...
- 《R语言实战》读书笔记-- 第六章 基本图形
首先写第二部分的前言. 第二部分用来介绍获取数据基本信息的图形技术和统计方法. 本章主要内容 条形图.箱型图.点图 饼图和扇形图 直方图和核密度图 分析数据第一步就是要观察它,用可视化的方式是最好的. ...
- 《Python基础教程》 读书笔记 第六章 抽象 函数 参数
6.1创建函数 函数是可以调用(可能包含参数,也就是放在圆括号中的值),它执行某种行为并且返回一个值.一般来说,内建的callable函数可以用来判断函数是否可调用: >>> x=1 ...
- 《利用python进行数据分析》读书笔记--第六章 数据加载、存储与文件格式
http://www.cnblogs.com/batteryhp/p/5021858.html 输入输出一般分为下面几类:读取文本文件和其他更高效的磁盘存储格式,加载数据库中的数据.利用Web API ...
- Spring AOP (Spring 3.x 企业应用开发实战读书笔记第六章)
从面相对象编程到面相切面编程,是一种代码组织方式的进化. 每一代的代码组织方式,其实是为了解决当时面对的问题.比如写编译器和写操作系统的时候的年代当然要pop,比如写界面的时候当然要oop,因为界面这 ...
- 《Java核心技术(卷一)》读书笔记——第六章:内部类
1. 内部类的概念? 类中类 2. 为什么要用内部类? 内部类的方法可以访问外部类的实例域 内部类对外部类的同一个包中的类实现了隐藏 匿名内部类在“想要定义一个回调函数却又不想编写 ...
随机推荐
- JavaWeb高级编程(上)
好久没更新了,发一篇以前记录学习的笔记. 面向读者:已经具有丰富的Java语言和Java SE平台知识的软件开发者和软件工程师. 预掌握知识: Internet.TCP.HTTP协议 HTML(5) ...
- Redhat6.7 切换Centos yum源
转自:http://inlhx.iteye.com/blog/2336729 RedHat 更换Yum源 1.检查yum包 rpm -qa |grep yum 2.删除自带包 rpm -aq | gr ...
- 浅谈DFS,BFS,IDFS,A*等算法
搜索是编程的基础,是必须掌握的技能.--王主任 搜索分为盲目搜索和启发搜索 下面列举OI常用的盲目搜索: 1.dijkstra 2.SPFA 3.bfs 4.dfs 5.双向bfs 6.迭代加深搜索( ...
- Stopping service [Tomcat] Disconnected from the target VM, address:XXXXXX解决方案
原文出处:https://blog.csdn.net/u013294097/article/details/90677049 Stopping service [Tomcat] Disconnecte ...
- 文件系统(01):基于SpringBoot框架,管理Excel和PDF文件类型
本文源码:GitHub·点这里 || GitEE·点这里 一.文档类型简介 1.Excel文档 Excel一款电子表格软件.直观的界面.出色的计算功能和图表工具,在系统开发中,经常用来把数据转存到Ex ...
- 技术派-如果编译提示winnt.h(222):error C2146错误
如果编译的时候,出现如下错误: \Microsoft Studio 8\VC\PlatformSDK\include\winnt.h(222):error C2146: 语法错误:缺少“:”(在标识符 ...
- Scala 学习(8)之「trait (2) 」
trait调用链 Scala 中支持让类继承多个 trait 后,依次调用多个 trait 中的同一个方法,只要让多个 trait 的同一个方法中,在最后都执行super.方法即可 类中调用多个 tr ...
- ros机器人之动作(二)
前面我们实现了动作的定义,接下来实现动作的功能 实现一个基本的动作服务器 准备好所需的动作定义后就可以开始编写代码了.动作和话题一样,都是使用回调机制,即回调函数会在收到消息时被唤醒和调用. 例:si ...
- pyhton3之进程
一 什么是进程 首先要了解什么是进程,程序是写出来没有被执行的代码,它是没有生命的实体,只有处理器赋予程序生命时,及程序被操作系统运行起来,他是一个活动的实体,我们称其为进程. 进程是计算机中的程序关 ...
- Codeforces_729_C
http://codeforces.com/problemset/problem/729/C 二分找最小容量,然后找符合的最小花费. #include<iostream> #include ...