#include<stdio.h> #include<iostream> #include<queue> #include<map> #include<memory.h> #include <math.h> #include <stdlib.h> #include <algorithm> #include <climits> #include <sstream> #include <cstdlib> using namespace std; /** * Calling a function with parameters taken by value causes copies of the values to be made. * This is a relatively inexpensive operation for fundamental types such as int, * but if the parameter is of a large compound type, * it may result on certain overhead. * For example, consider the following function: */ /** *調用傳值函數的時候複製值到函數棧上,這個操做對於基本類型(like int)來講花費不多. *可是若是參數大的複合類型,花費就很大,好比下面這個函數. *函數的參數是倆個string(按值傳遞),返回的結果是倆個字符串拼接. *按值傳遞中,複製a和b的內容給函數,若是參數是長的字符串,這意味着調用函數時複製了大量的數據 */ string concatenate(string a, string b) { return a + b; } /** * 比較好的方式以下 * 按引用傳遞不須要複製數據.函數操做直接在原始字符上(只是給它起了一個別名). *頂多這意味着給函數傳遞某些指針. * */ string concatenate2(string& a, string& b) { return a + b; } /** * 按引用傳遞可能被調用的函數會修改引用的內容,咱們能夠這樣解決 */ string concatenate3(const string& a,const string& b) { return a+b; } /** * that for most fundamental types, * there is no noticeable difference in efficiency, and in some cases, * const references may even be less efficient! * * 對於基本類型,效率並不會有多大差異,在某些狀況下,常量引用可能效率更低 */ /** * 參數的默認值 */ int divide(int a,int b=2) { int r; r = a/b; return (r); } int main(const int argc, char** argv) { //from cstdlib cout<<divide(12)<<endl; cout<<divide(20,4)<<endl; return EXIT_SUCCESS; }