C++ C++ 程程程程 程程程程
C++C++ 程式語言程式語言
2
C++C++ 程式語言 – 大綱程式語言 – 大綱1. 指標2. 結構與類別3. 標準函式庫 (STL)
3
指標指標 (pointer)(pointer)
int var = 4;int* ptr = &var; cout<<&var<<endl; //12FF88cout<<ptr<<endl; //12FF88Cout<<*ptr<<endl; //4Cout<< var<<endl; //4
int* ptr2;ptr2 = &var;
int* ptr;
int* ptr = &var;
指標
&取址運算子var
0012FF884
name
addressvalue
ptr
0012FF880012FF80
name
addressvalue
4
指標指標int var = 4;int* ptr = &var; cout<<var<<endl; //4*ptr = 5;cout<<var<<endl; //5Var = 6;cout<<*ptr<<endl; //6
int* ptr;
int* ptr = &var;
指標
&取址運算子
*取值運算子
var
0012FF884
name
addressvalue
ptr
0012FF880012FF80
name
addressvalue
5
問題 問題 11
//*(&var) 等於 *(ptr)cout<<*&var<<endl; //4cout<<&*var<<endl; //invalid
cout<<*&ptr<<endl; //0012FF88//&(*ptr) 等於 &(var)cout<<&*ptr<<endl; //0012FF88
var
0012FF884
name
addressvalue
ptr
0012FF880012FF80
name
addressvalue
6
參照參照 (reference)(reference)
int var = 4;int& ref = var; cout<<var<<endl; //4cout<<ref<<endl; //4
ref = 5;cout<<var<<endl; //5
var = 6;cout<<ref<<endl; //6
int& ref = var;
( 一定要初始化 )參照
ref
&取址運算子
*取值運算子
var
0012FF884
name
addressvalue
7
指標 – 指標做為函式引數指標 – 指標做為函式引數void swap_ref(int& a, int& b) {
int temp = a;a = b;b= temp;
}
void swap_ptr(int* a, int* b) {int temp = *a;*a = *b;*b= temp;
}
8
指標 – 指標做為函式引數指標 – 指標做為函式引數#include<iostream.h>
//主函式int main(void){int a =3, b=5;swap_ref(a,b);swap_ptr(&a,&b);
system(“PAUSE”);return 0; //回傳 0,程式結束
}
9
指標 – 指標運算指標 – 指標運算int var = 4;int* ptr = &var; cout<<ptr<<endl; //12FF88cout<<ptr+5<<endl; //12FF8Dcout<<ptr-5<<endl; //12FF83cout<<ptr++<<endl; //12FF89cout<<ptr--<<endl; //12FF87
ptr=ptr+5;
ptr=ptr-5;
ptr+=5;
Ptr-=5;
ptr++;
Ptr--;
指標運算
var
0012FF884
ptr
0012FF880012FF80
10
指標 – 陣列與指標間的關係指標 – 陣列與指標間的關係#include<iostream.h>
//主函式int main(void){
int a[]={1,2,3,4,5,6,7,8,9,10};cout<<*a<<endl; // 1cout<<*(a+1)<<endl; // 2cout<<*(a+2)<<endl; // 3cout<<*a+1<<endl; //
for(int i=0, i<9; i++){cout<<*(a+i);
} //
system(“PAUSE”);return 0;
}
1 2 3 4 5 6 7 8 9 10
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
1 2 3 4 5 6 7 8 9 10
a a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9
11
結構結構 (structure)(structure)
struct customer{
double x;double y;int demand;
};main(){
customer client;client.x = 10.5;client.y = 4.3;client.demand = 40;cout<<“(”<< client.x <<“,”<< client.y <<“)” <<endl;cout<<“’s demand is ”<< client.demand <<endl;
getchar();}
struct name{
......};
結構宣告
12
結構 – 結構陣列結構 – 結構陣列struct customer{
double x;double y;int demand;
};main(){
customer client[5];getchar();
}
13
結構與類別結構與類別 (class)(class)
class customer{public:
double x;double y;int demand;
};//兩者的宣告意義相同struct customer{
double x;double y;int demand;
};
class name{public:
......protected:
......private:
......};
結構宣告
14
練習 練習 11
練習建構一個學生資料的類別 ( 結構 ) ,包含:1. 學生學號2. 學生姓名3. 國文成績4. 數學成績
15
結構 – 成員函式結構 – 成員函式struct customer{
double x;double y;int demand;int distTo0(){return sqrt(x*x+y*y);}
};main(){
customer client;client.x = 10.5;client.y = 4.3;client.demand = 40;cout<<“(”<< client.x <<“,”<< client.y <<“)” <<endl;cout<<“’s demand is ”<< client.demand <<endl;cout<<client.distTo0();
getchar();}
16
練習 練習 22
承上題的學生資料的類別 ( 結構 ) ,增加一個會回傳平均成績的成員函式。
17
結構 – 建構子結構 – 建構子 (constructor)(constructor)struct customer{
double x;double y;int demand;customer();customer(double xx, double yy, int dd){x=xx; y=yy; demand=d;}int distTo0(){return sqrt(x*x+y*y);}
};main(){
customer client(10.5, 4.3, 40);cout<<“(”<< client.x <<“,”<< client.y <<“)” <<endl;cout<<“’s demand is ”<< client.demand <<endl;cout<<client.distTo0();getchar();
}
18
補充 – 命名空間補充 – 命名空間 (namespace)(namespace)
namesapce math{
int POWER(int a, int b);double SQRT(double n);int ABS(int n);
};
main(){int a=2;math::POWER(2,6);math::SQRT(2,6);
}
namespace name{
......};
命名空間
using namespace name;
使用命名空間
19
補充 – 命名空間補充 – 命名空間 (namespace)(namespace)
namesapce math{
int POWER(int a, int b);double SQRT(double n);int ABS(int n);
};
using namesapce math;
main(){int a=2;POWER(2,6);SQRT(2,6);
}
namespace name{
......};
命名空間
using namespace name;
使用命名空間
20
標準程式庫標準程式庫 (standard template library; STL)(standard template library; STL)
#include<iostream> //不用 .h#include<fstream>#include<string>
using namesapce std;
main(){cout<<“STL”<<endl;getchar();
}
using namespace std;STL命名空間
21
標準程式庫標準程式庫 (STL) – vector (STL) – vector 動態陣列動態陣列#include<vector>int main(void){
vector<int> vct1(10);vector<int> vct2(10,2);vector<int> vct3;vct3.resize(10);
int size=10;vector<int> vct4(size);vct4.resize(12);
system(“PAUSE”);return 0;
}
vector<type> var
vector 宣告
22
標準程式庫標準程式庫 (STL) – vector (STL) – vector 動態陣列動態陣列#include<vector>#include<iostream>using namespace std;int main(void){
vector<int> vct(10);int size = vct.size(); //陣列大小vct[0]=100; //存入陣列cout<<vct[0]<<endl; //取得陣列數值system(“PAUSE”);return 0;
}
23
標準程式庫標準程式庫 (STL) – vector (STL) – vector 動態陣列動態陣列#include<vector>#include<iostream>using namespace std;int main(void){
vector<int> vct(3,2);vct.push_back(6);vct.push_front(6);vct.insert(vct.begin()+3, 5);vct.erase(vct.begin()+4);
system(“PAUSE”);return 0;
}
24
標準程式庫標準程式庫 (STL) – algorithm(STL) – algorithm
方法 說明sort 排序
random_shuffle 亂數排序next_enumeration 下一個列舉數prev_enumeration 上一個列舉數
find 搜尋
#include<algorithm>