利用结构体定义一个加法以及自定义输出
2014-08-11 15:28
411 查看
原与紫书。
模板:
#include<cstdio> #include<string> #include<cmath> #include<queue> #include<vector> #include<sstream> #include<cstring> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; struct Point { int x, y; Point( int x=0, int y=0 ) : x(x), y(y){} //相当于Point( int x=0, int y=0 ) { this->x = x, this->y = y; } }; Point operator + ( const Point& A, const Point& B ) { return Point( A.x+B.x, A.y+B.y ); } ostream& operator << ( ostream &out, const Point& p ) { out << "(" << p.x << "," << p.y << ")"; return out; } int main() { Point a, b(1, 2); a.x = 3; cout << a+b << endl; return 0; }
模板:
#include<cstdio> #include<string> #include<cmath> #include<queue> #include<vector> #include<sstream> #include<cstring> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; template <typename T> struct Point { T x, y; Point( T x=0, T y=0 ) : x(x), y(y) {} }; template <typename T> Point<T> operator + ( const Point<T>& A, const Point<T>& B ) { return Point<T>( A.x + B.x, A.y + B.y ); } template <typename T> ostream& operator << ( ostream &out, const Point<T>& p ) { out << "(" << p.x << "," << p.y << ")"; return out; } int main() { Point<int> a(1, 2), b(3, 4); Point<double> c( 1.1, 2.2 ), d( 3.3, 4.4 ); cout << a+b << " " << c+d << endl; return 0; }
相关文章推荐
- Golang 如何定义一个接口类型的切片,它可以用来存储混合类型的数据,又如何自定义错误信息输出,以及如何定义变参函数,还有字符串多种拼接方式
- 问题十四:定义一个结构体变量(包括年、月、日),要求输入年、月、日,计算输出该日是该年的第几天。
- 定义一个结构体变量,输入数据,输出变量内容。
- 利用头文件ctime,并且定义一个类,来实现输出当前时间
- 例题:把一个超市购物的题做成函数。这个题做成函数没有必要,但可以了解输入参数,输出参数,返回值,函数体,还有结构体,ArryList集合,for循环算结果,以及集合里 .count的使用
- 给定一个自定义函数,random5() 可以随机生成1-5之间的随机数,请利用rondom5()定义一个rondom
- 定义一个结构体变量(包括年、月、日),编写程序,要求输入年月日,程序能够计算并输出改日是本年中的第几天。注意闰年问题。(switch语句)
- 20150920结构体 定义一个学生的结构体,学号,姓名,身高,输入学生信息,按身高排序输出;
- Html Table用JS导出excel格式问题 导出EXCEL后单元格里的000412341234会变成412341234 7-14 会变成 2018-7-14(7月14) 自定义格式 web利用table表格生成excel格式问题 js导出excel增加表头、mso-number-format定义数据格式 数字输出格式转换 mso-number-format:"\@"
- 第九周 项目1 在第八周基础上定义一个流输入输出以及-c
- 定义一个不受计算机字长限制的整数类INT,要求INT与INT以及INT与C++基本数据类型int之间能进行+、-、×、÷和=运算,并且能通过cout输出INT类型的值。(持续添加)
- 定义一个结构体变量(包括年、月、日),编写程序,要求输入年月日,程序能够计算并输出改日是本年中的第几天。注意闰年问题。(数组)
- 我在一个结构体里定义超过两个字符数组的成员输出结果会出错
- C#下面,怎么把一个自定义的结构体写入到文件中去?
- 输出一个数全排列的组合【利用哈密尔顿图遍历算法实现】
- WSS3SDK之:通过Feature创建一个自定义列表定义
- 结构体的定义以及使用
- 利用自定义事件结合ModalPopup写一个通用的MsgBox类
- sqlserver利用sp_oa系列存储过程调用xml+xsl eval输出运算结果的一个例子。