essential c++ 读书笔记1

ESSENTIAL C++ 

chapter  2

1,final implement of fib_elem() 

 

bool fibon_elem(int pos, int &elem){
  if(pos<=0||pos >=1024)
//.........................
  elem=1;
  int n1=1,n2=1;
  for(int ix=3;ix<=pos;ix++)
  {
      elem=n1+n2;
      n2=n1;
      n1=elem;
  }
  return true;
}

2, passing by value:(make a copy)    OR     passing by reference:(passing the address)

example :    swap(int a, int b)  OR   swap(int &a, int &b)

3,passing a pointer   OR   passing a reference parameter

passing a pointer:   before we dereference a pointer, we must always make sure it is NOT 0;

4, dynamic memory management

 

int *p;
p=new int(100);  // set an initial value
delete p;
int *pia=new int[24];  // allocate an array of heap elements ; means int[24] pia;
delete[] pia;

5,  reference to an object

int ival=12;

int &rval=ival;

reference can not be re-assigned to other object

6,providing a default parameter value

 

void bubble_sort(vector<int> &vec., ofstream *ofil=0){} 
//set pointer ofil to 0

unlike pointer, reference can not be set to 0

7,

void display (const vector<int> &vec, ostream &os=cout){}

2 RULES about providing default parameter:

1, default values are resolved beginning with right most parameter ,if a parameter is provided a default value,all the parameter to its right must have a default

2, the default value can be specified only once

  we can specify the default value in header files 

 

8, local static object

 

const vector<int >*    fibon_seq(int size)
{
static vector<int> elems; //elems is local static object, elems is no longer
                                        //destroyed or re-created each invocations of fibon
elems.push_back(1);
return &elems;
}

9, Inline function

 

inline bool fub(){}

10, overloaded function :(function must be unique)

11, template function

template<typename elemType>

vector<elemType> a;

12; Pointer to function

 

bool seq_elem(int pos, int &elem, const vector<int>*   (*seq_ptr)(int))   //to recognize seq_ptr as a pointer
{
     const vector<int> *p=seq_ptr(pos);    //invoke the function
     elem=(*p)[1];   
}

//you can also you a array to store function pointer
const vector<int>* fib(int size);
const vector<int>* (*seq_array[])(int)={fib, .......};
seq_ptr=seq_array[1];


13, header file

if the header file is in the same directory as the program text file, we use " "  

else we use <>

 

//header file called num.h
//place in it some declaration 
const vector<int>* fib(int size);
//  elem=(*fib)[1]
extern const vector<int>* (*seq[2])(int); // extern  turn the definition to   
                                                                  //declaration                                

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/ggppwx/archive/2010/05/22/1741620.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值