struct、object、指標、參考、運算子重載
講師:張秉中
#include <bits/stdc++.h>
https://gist.github.com/Einstrasse/ac0fe7d7450621a39364ed3b05cacd11
pointer
#include <iostream>
using namespace std;
int main(){
int *a,*b;
a = new int(5);//new ___ 的回傳值為指標
int c = 8;
b = &c;
cout<<(*a)+(*b)<<endl;
b = a;
*a = 10;
cout<<(*a)<<' '<<(*b)<<endl;
}
#include <iostream>
using namespace std;
int main(){
int *a,*b;
a = new int(5);//new ___ 的回傳值為指標
int c = 8;
b = &c;
cout<<(*a)+(*b)<<endl;
b = a;
*a = 10;
cout<<(*a)<<' '<<(*b)<<endl;
}
13
10 10
reference
#include <iostream>
using namespace std;
int main(){
int b = 10;
int &a = b;
a = 13;
cout<<a<<' '<<b<<endl;
b = 48763;
cout<<a<<' '<<b<<endl;
}
13 13
#include <iostream>
using namespace std;
int main(){
int a = 5;
int *b= &a;
int* &c = b;
*c = 7414;
cout<<a<<' '<<*b<<' '<<*c<<endl;
int** d = &c;
int *e= new int(10);
*d = e;
cout<<a<<' '<<*b<<' '<<*c<<' '<<*(*d)<<' '<<*e<<endl;
}
#include <iostream>
using namespace std;
int main(){
int a = 5;
int *b= &a;
int* &c = b;
*c = 7414;
cout<<a<<' '<<*b<<' '<<*c<<endl;
int** d = &c;
int *e= new int(10);
*d = e;
cout<<a<<' '<<*b<<' '<<*c<<' '<<*(*d)<<' '<<*e<<endl;
}
#include <iostream>
using namespace std;
int main(){
int a = 5;
int *b= &a;
int* &c = b;
*c = 7414;
cout<<a<<' '<<*b<<' '<<*c<<endl;
int** d = &c;
int *e= new int(10);
*d = e;
cout<<a<<' '<<*b<<' '<<*c<<' '<<*(*d)<<' '<<*e<<endl;
}
function
...
#include <bits/stdc++.h>
using namespace std;
string check(string account,int password){
if(account == "pcc"&&password == 48763)return "correct";
else return "error";
}
int main(){
cout<<check("pcc",48763)<<endl;
cout<<check("pccc",123)<<endl;
}
#include <bits/stdc++.h>
using namespace std;
void swap_value(int *a,int *b){
int c = *b;
*b = *a;
*a = c;
return;
}
int main(){
int x = 10,y = 5;
swap_value(&x,&y);
cout<<x<<' '<<y<<endl;
}
#include <bits/stdc++.h>
using namespace std;
void swap_value(int &a,int &b){
int c = b;
b = a;
a = c;
return;
}
int main(){
int x = 10,y = 5;
swap_value(x,y);
cout<<x<<' '<<y<<endl;
}
#include <bits/stdc++.h>
using namespace std;
int x = 5;
auto add = [](int a,int b){
return a+b;
};
int main(){
int arr[] = {1,2,3,4,5};
for(int i = 0;i<5;i++){
cout<<add(arr[i],x)<<',';
}
cout<<endl;
cout<<x<<endl;
auto minus = [&arr,&x](){
for(int i = 0;i<5;i++)arr[i] -=x;
x--;
return;
};
minus();
for(int i = 0;i<5;i++)cout<<arr[i]<<',';
cout<<endl;
cout<<x<<endl;
minus();
for(int i= 0;i<5;i++)cout<<arr[i]<<',';
cout<<endl;
cout<<x<<endl;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int x = 0,y = 0;
auto g = [](int a,int b){
return a+b;
};
auto f = [&g](){
return g(g(1,2),g(1,2));
};
cout<<f(g);
}
本頁以下的內容基本上很少用到(或幾乎用不到)
不會也不會怎樣
(但是右邊那幾頁還是要學喔)
Function pointer
#include <bits/stdc++.h>
using namespace std;
string f(int a,int b){
return string(1,'A'+a-1)+(char)('A'+b-1);
}
int main(){
string (*func_ptr)(int,int) = f;
cout<<func_ptr(1,3);
}
Function pointer
#include <bits/stdc++.h>
using namespace std;
string f(int a,int b){
return string(1,'A'+a-1)+(char)('A'+b-1);
}
string g(int c,int d){
return "hi";
}
int main(){
string (*func_ptr)(int,int) = f;
string (*func_ptr2)(int,int) = g;
vector<string(*)(int,int)> v;
v.push_back(func_ptr);
v.push_back(func_ptr2);
for(int i = 0;i<v.size();i++){
cout<<v[i](1,3)<<endl;
}
}
#include <bits/stdc++.h>
using namespace std;
string f(int a,int b){
return string(1,'A'+a-1)+(char)('A'+b-1);
}
string g(int c,int d){
return "hi";
}
int main(){
auto k = [](int a,int b){
return (string)"AcWaTle";
};
string(*func_ptr1)(int,int) = f;
string(*func_ptr2)(int,int) = g;
vector<string(*)(int,int)> v = {f,g,k,[](int a,int b){
return (string)"HIHI";}};
for(auto &i:v){
cout<<i(1,3)<<endl;
}
}
Text
struct student{
int height,birthday;
float weight;
char name[30];
};
/*-------------------------------*/
#include <string>
struct student{
string student_id;
float weight;
};
Text
#include <iostream>
#include <string>
using namespace std;
struct student{
int height;
float weight;
float bmi;
string name;
student(){
height = 0;
weight = 0;
bmi = 0;
name = "unknown";
}
student(int h,float w){
height = h;
weight = w;
bmi = weight/(height/100.0)/(height/(100.0));
}
void rename(string s){
name = s;
}
};
int main(){
student rata(172,45.0);
rata.rename("ckpc_rata");
cout<<rata.name<<' '<<rata.bmi;
}
#include <iostream>
#include <string>
using namespace std;
struct wheel{
int radius;
wheel(){}
};
class car{
public:
wheel wheels;
string name;
int x,y;
void move(int dx,int dy){
x += dx;
y += dy;
}
car(int px,int py,wheel w,string s){
wheels = w;
x = px;
y = py;
name = s;
}
car(){
x = y = 0;
name = "unknown";
}
private:
string id;
};
int main(){
wheel ww;
ww.radius = 20;
car bmw(0,0,ww,"my car");
cout<<bmw.name<<' '<<bmw.wheels.radius<<endl;
bmw.move(1,2);
cout<<bmw.x<<' '<<bmw.y<<endl;
// cout<<bmw.id<<endl; ??????private???
}
inheritance
#include <bits/stdc++.h>
using namespace std;
#define ll long long
class A{
public:
int x,y,z;
A(){
x = y = z = 0;
}
void getval(){
cout<<x<<y<<z<<endl;
}
};
class B:public A{
public:
B(){
x = y = z = 1;
}
};
int main(){
A* a = new B;
a->getval();
}
自訂標頭檔
編譯指令:g++ -std=c++11 -c ___.o test.h test.cpp others.cpp
g++ -std=c++11 -o test.exe ___.o _____.o _.o(or just *.o)
#include<bits/stdc++.h>
using namespace std;
class CK{
public:
CK();
vector<string> students;
string principal;
int get_students();
void change_principal(string s);
};
#include "test.h"
#include <bits/stdc++.h>
using namespace std;
CK::CK(){
for(int i = 1;i<=33;i++){
students.push_back(to_string(i));
}
}
void CK::change_principal(string s){
principal = s;
}
int CK::get_students(){
return students.size();
}
int main(){
int c;
cin>>c;
return 0;
}
這也很少用到,通常用來除錯或打模版
#include <bits/stdc++.h>
using namespace std;
struct point{
int x,y;
point(){
x = y = 0;
}
point(int xx,int yy){
x = xx,y = yy;
}
point operator+(point b){
return point(x+b.x,y+b.y);
}
point operator-(point b){
return point(x-b.x,y-b.y);
}
int operator*(point b){
return x*b.x+y*b.y;
}
point operator*(int k){
return point(k*x,k*y);
}
};
point operator+=(point &a,point &b){//也可以寫在struct外
a.x += b.x;
a.y += b.y;
return a;
}
int main(){
point a(1,2);
point b(2,-1);
cout<<a.x<<' '<<a.y<<' '<<(a+b).x<<' '<<a*b<<' '<<(a*3).x<<endl;
}
#include <iostream>
using namespace std;
struct point{
int x,y;
point(){
x = y = 0;
}
point(int xx,int yy){
x = xx,y = yy;
}
bool operator<(const point &b)const{//比大小先比x在比y
if(x != b.x)return x<b.x;
else return y<b.y;
}
};
istream& operator>>(istream& in,point &a){
in>>a.x>>a.y;
return in;
}
ostream& operator<<(ostream& out,point a){
out<<a.x<<' '<<a.y;
return out;
}
int main(){
point a(1,2);
point b(2,-1);
cin>>a;
cout<<a<<endl;
}
這些之後學排序很好用
可學可不學的
auto
#include <bits/stdc++.h>
運算子重載
設計一個一維線段的struct line
在main裡加入這堆要可以跑才算成功
line a(1,100);
line b(10,11);
a = a+b;
cout<<a<<endl;
line c(0,a.end);
c.split(a.start)
cout<<c.size()
寫一個計算2*2矩陣的struct