美妝網(wǎng)站開(kāi)發(fā)規(guī)劃書(shū)今日國(guó)際新聞最新消息大事
1、函數(shù)
函數(shù)是對(duì)實(shí)現(xiàn)某一功能的代碼的模塊化封裝。
?函數(shù)的定義:
標(biāo)準(zhǔn)函數(shù):
輸入 n 對(duì)整數(shù)的 a、b ,輸出它們的和。
#include <iostream>
#include <windows.h>
using namespace std;int add(int a,int b);//函數(shù)原型聲明int main()
{int n,a,b;cin>>n;int *c=new int[n];//動(dòng)態(tài)數(shù)組for(int i=0;i<n;i++){cin>>a>>b;c[i]=add(a,b);//調(diào)用函數(shù)}for(int i=0;i<n;i++){cout<<c[i]<<endl;}return 0;
}int add(int a,int b){//函數(shù)定義return a+b;
}
運(yùn)行結(jié)果如下:
?無(wú)返回值:
輸入n,輸出1~n之間所有整數(shù)。
#include <iostream>
#include <windows.h>
using namespace std;void print(int n);//函數(shù)原型聲明int main()
{int n;cin>>n;print(n);return 0;
}void print(int n){//函數(shù)定義for(int i=1;i<=n;i++){cout<<i<<endl;}
}
運(yùn)行結(jié)果:
?無(wú)參數(shù):
輸入n,如果n為10的倍數(shù),輸出3個(gè)“very good!”。
#include <iostream>
#include <windows.h>
using namespace std;void print();//函數(shù)原型聲明int main()
{int n;cin>>n;if(n%10==0){print();}return 0;
}void print(){//函數(shù)定義for(int i=1;i<=3;i++){cout<<"very good!"<<endl;}
}
?運(yùn)行結(jié)果如下:
?傳值參數(shù):
輸入兩個(gè)整數(shù)a,b,交換后輸出。
#include <iostream>
#include <windows.h>
using namespace std;void swap(int x,int y);//函數(shù)原型聲明int main()
{SetConsoleOutputCP(CP_UTF8);int a,b;cin>>a>>b;swap(a,b);cout<<"a和b交換后"<<a<<"\t"<<b<<endl;return 0;
}void swap(int x,int y){//傳值參數(shù)定義int temp;temp=x;x=y;y=temp;
}
?發(fā)現(xiàn)傳值參數(shù)調(diào)用,只在函數(shù)內(nèi)有效,離開(kāi)函數(shù),參數(shù)并未交換值。
引用參數(shù):
比如:int &b = a;?在定義b的時(shí)候前面的符號(hào)& 就表示b是a的一個(gè)引用,對(duì)引用變量的賦值等操作相當(dāng)于對(duì)變量本身的操作。所以你對(duì)b賦值 b=a+14其實(shí)相當(dāng)于a=a+14操作,引用又稱(chēng)為別名,相當(dāng)于一個(gè)人兩個(gè)名字。所以b是15。
?接下來(lái)我們?cè)趨?shù)前面加個(gè)&號(hào),代表引用參數(shù),改變參數(shù)值,跳出函數(shù)依然生效。
?數(shù)組參數(shù):
輸入n個(gè)數(shù)存入數(shù)組a[]中,求和后輸出和值。
#include <iostream>
#include <windows.h>
using namespace std;
//int a[100];//定長(zhǎng)數(shù)組long long sum(int s[],int n){//參數(shù)數(shù)組long long count=0;for(int i=0;i<n;i++){count+=s[i];}return count;
}
int main()
{SetConsoleOutputCP(CP_UTF8);int n;cin>>n;int *a = new int[n];//動(dòng)態(tài)數(shù)組for(int i=0;i<n;i++){cin>>a[i];}cout<<"sum 計(jì)算結(jié)果"<<sum(a,n)<<endl;return 0;
}
?運(yùn)行結(jié)果(這里我輸入10個(gè)數(shù)):
?字符串參數(shù):
輸入n個(gè)字母,如果是小寫(xiě)字母將其轉(zhuǎn)換為大寫(xiě)字母,輸出轉(zhuǎn)換后的字符串。
#include <iostream>
#include <windows.h>
#include <cstring>
using namespace std;string covert(string &s)
{for(int i=0;i<s.length();i++){if(s[i]>='a'&&s[i]<='z'){s[i]-=32;//將小寫(xiě)字符轉(zhuǎn)為大寫(xiě)}}return s;
}
int main()
{SetConsoleOutputCP(CP_UTF8);string s;cin>>s;covert(s);cout<<s<<endl;return 0;
}
?運(yùn)行結(jié)果:
?函數(shù)嵌套:
#include <iostream>
#include <windows.h>
#include <cstring>
using namespace std;int gcd(int x,int y)//最大公約數(shù)
{int t;t=x%y;//求余數(shù) while(t!=0){x=y;//y做被除數(shù) y=t;//余數(shù)做除數(shù) t=x%y;//求余數(shù) }return y;
}
int lcm(int x,int y){//最小公倍數(shù)return x*y/gcd(x,y);
}
int main()
{SetConsoleOutputCP(CP_UTF8);int a,b;cin>>a>>b;cout<<"最大公約數(shù):"<<gcd(a,b)<<endl;cout<<"最小公倍數(shù):"<<lcm(a,b)<<endl;return 0;
}
運(yùn)行結(jié)果如下:
?函數(shù)重載(多態(tài)):
多個(gè)同名函數(shù)(參數(shù)數(shù)目、類(lèi)型、順序不同)
寫(xiě)一個(gè)函數(shù),對(duì)于字符串類(lèi)型數(shù)據(jù)取其長(zhǎng)度的一半,對(duì)于浮點(diǎn)數(shù)類(lèi)型,求其值的二分之一。
?
#include <iostream>
#include <windows.h>
#include <cstring>
using namespace std;float half(float a){return a/2;
}
string half(string s){int n=s.length()/2;char *str=new char[n];for(int i=0;i<n;i++){str[i]=s[i];}return str;
}
int main()
{SetConsoleOutputCP(CP_UTF8);float a;string b;cin>>a>>b;cout<<"float 的一半值:"<<half(a)<<endl;cout<<"string 的一半值:"<<half(b)<<endl; return 0;
}
?運(yùn)行結(jié)果:
?函數(shù)模版:
-
C++另一種編程思想稱(chēng)為?泛型編程?,主要利用的技術(shù)就是模板
-
C++提供兩種模板機(jī)制:函數(shù)模板和類(lèi)模板
函數(shù)模板作用:
建立一個(gè)通用函數(shù),其函數(shù)返回值類(lèi)型和形參類(lèi)型可以不具體制定,用一個(gè)虛擬的類(lèi)型來(lái)代表。
函數(shù)模版語(yǔ)法:
template<typename T>
T add(T x,T y)
{return x+y;}
解釋:
template --- 聲明創(chuàng)建模板
typename --- 表面其后面的符號(hào)是一種數(shù)據(jù)類(lèi)型,可以用class代替
T --- 通用的數(shù)據(jù)類(lèi)型,名稱(chēng)可以替換,通常為大寫(xiě)字母
函數(shù)模版使用示例:
//交換整型函數(shù)
void swapInt(int& a, int& b) {int temp = a;a = b;b = temp;
}//交換浮點(diǎn)型函數(shù)
void swapDouble(double& a, double& b) {double temp = a;a = b;b = temp;
}//利用模板提供通用的交換函數(shù)
template<typename T>
void mySwap(T& a, T& b)
{T temp = a;a = b;b = temp;
}void test01()
{int a = 10;int b = 20;//swapInt(a, b);//利用模板實(shí)現(xiàn)交換//1、自動(dòng)類(lèi)型推導(dǎo)mySwap(a, b);//2、顯示指定類(lèi)型mySwap<int>(a, b);cout << "a = " << a << endl;cout << "b = " << b << endl;}int main() {test01();system("pause");return 0;
}
總結(jié):
- 函數(shù)模板利用關(guān)鍵字 template
- 使用函數(shù)模板有兩種方式:自動(dòng)類(lèi)型推導(dǎo)、顯示指定類(lèi)型
- 模板的目的是為了提高復(fù)用性,將類(lèi)型參數(shù)化