#include<iostream>
using namespace std;
struct point{ // 结构体,里面有成员变量和构造函数
int x,y;
point(int m = 0,int n = 0){
x = m;
y = n;
}
};
point operator +(const point& a,const point& b){ // 重新定义了point的+法
return point(a.x+b.x,a.y+b.y);
}
ostream& operator <<(ostream& out,const point& a){ // 重新定义了输出流的<<符号
out << "(" << a.x << "," << a.y << ")";
return out;
}
int main(){
point a,b;
b.x = 1;
b.y = 2;
cout << a+b << endl;
}
理解:结构体是不能传参的,但结构体里面的同名成员函数是为了初始化这个结构里的成员变量。
----------------------------------------------------------------------------------------------------------------------------
C里面的指针:
#include<stdio.h>
using namespace std;
void swap(int* m,int* n){
int t;
t = *m;
*m = *n;
*n = t;
}
int main(){
int a = 2;
int b = 3;
swap(&a,&b);
cout << a << b << endl;
}
理解:给swap传入a,b的地址;m = &a,n = &b;*m = a,*n = b,并不是简单的赋值,而是代表着变量;然后进行交换的操作;
C++里面的指针引用:
#include<iostream>
using namespace std;
void swap(int& a,int& b){
int t;
t = a;
a = b;
b = t;
}
int main(){
int a = 3;
int b = 4;
swap(a,b);
cout << a << b << endl;
}
理解:&有引用的用处,此时的a、b与main里的a、b是同个变量
C++结构体(算法经典)
原文:https://www.cnblogs.com/xiaoxuxuxu/p/12245284.html