Essential_C++ of mine


//===================================================================================//

FAQ
1. could template functions include more than one typename parameter?
   Of course ok.

2. Could iterator be forced convert to other pointer or type?

3. default parameter , when invokeing, pass no default, or pass different value, what happen ?

4. vector<int>  iVect;
   iVect.push_back( other_vect[i] );
  
   Did the iVect( null ) be allocated when defintion or .push_back() ?
  
   

5. Member initializtion list  same as constructor ?
   好处是好像可以少一次对象的构造.

6. 继承垂直体系中,要是某个中间级别的class没有重新实现某个virtual function,在基类指针指向的该派生类对象调用此virtual function时,会怎样?

7. Page 183, 指向派生类对象的基类指针为何不能调用派生类的函数?是否因为该基类指针是虚基类?

//===================================================================================//

<< PART 1-2 >>

1. using namespace

2. string class; <string>  // stdlib

3. const var;

4. Using the Array and Vector  // section 1.5
   vector class; <vector>  // stdlib
   <a> vector initialize;
     (1) assign every elememts
     (2) initialize by array : vector <int> elem_seq( elem_vals, elem_vals+seq_size );

   <b> .size() in vector;

5. using pointer by vector
   vector <int> *pv = 0;
   vector <int> *seq_addrs[ 6 ] = {
  &fibonacci, &lucas, &pell,
  &triangular, &square, &pentagonal
  };

6. pseudo random number
 
   Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.

   srand( time(NULL) );
   index = rand() % range;
 
7. file I/O
    7.1 stream obj smart read/write without format control.
    7.2 could open, but don't need close.   
    7.3 manipulator, e.g. "endl"
   
    <a> ofstream  for output
    <b> ifstream  for input
    <c> fstream   for I/O
   
    note: When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.


8. pass by reference
    two target of using 
   <1> pass class objects
   <2> modify pass value
   <3> operator over

9. new / delete type
   don't need judge the ptr void

10.Default parameter value
   Using : [Page 70]

   Rule 1: begin default parameter at the right
   Rule 2: default only be assigned once, declaration or definition. But only once!
   e.g.
    declaration in "display.h"
 void display( const vector<int> &, ostream &= cout );

    definition in cfile
        #include "display.h"
 void display( const vector<int> &vec, ostream & os );
 {
      // ...;
 }


11. local static objects

12. inline functions

13. function overloading
    Different by parameter list, not by the return value!

14. Template functions
    The parameter of Template functions general include two types:
    "The explicit" and "the confirm later" !

    template functions could be overloading too.

15. pointer to functions

    const vector<int>* (*seq_array[]) ( int ) =  {
      a_seq, b_seq, c_seq
       };

<< PART 3 >>

1. iterator ; like pointer ?
   iterator depend on :<1> type of container, <2> type of element;
  
   iterator could be ++, like this:
   vector<int>:: const_iterator c_iter = vec.begin();
   ++c_iter;   // pointer to the next element


   
2. Squential Containers
   <a>  <vector>
    <list>
    <deque>

   <b> Define object of sqential containers by 5 ways:
 <1> list<int>  iList;    // NULL container
 <2> vector<string> sVect( 256 );  // size is 256, initialized value by default value
 <3> vector<int> ( 64, -1 );   // size is 64, initialized value is -1
 <4> int ia[8] = {1, 1, 2, 3, 5, 8, 13, 21 };
     vector<int> ( ia, ia+8 );   // init by iterators
 <5> list<string> sList_1;
     // fill slist_1 here ...
     list<string> sList_2 ( sList_1 );

   <c> operations :
 <1> push_back()
 <2> pop_back()
 <3> push_front() // exclude <vector>
 <4> pop_front()  // exclude <vector>
 <5> front() // only for read
 <6> back() // only for read
 <7> insert()
 <8> sort()
 <9> copy()
 
   <d> algorithm
 <1> find()
 <2> binary_search()
 <3> count()
 <4> search()

3. Function Objects
 Need Function Objects in the cause of efficiency !  

 template< class InputIterator, class T, class Predicate > inline
    InputIterator find_if(
        InputIterator First,
        InputIterator Last,
        Predicate Predicate
     );
 like for( InputIterator First, InputIterator Last, ++ )
  if( Predicate ) return ;


4. Function object adapter
    binder adapter : bind1st(); bind2nd();

 e.g. find_if() need unary function object, but less<int> is binary function object, so using bind2nd( less<int>, val ) do it!

 /* I think that find_if() would be expanded when compiling, less<int> have two input value, the compiler could not know which is by container, which is judging value. So we must use bind2nd to confirm it. Of course, Maybe use user defined function is more simple than less<int> here! */

       Note: see page 108. It is summary of this part !

5. Map
    Every element is Key-Value pair.
 
 e.g.
 #include <map>
 #include <string>

 string  tword;
 map< string, int > words;

 words[ tword ]; // auto insert if tword is not exist in this map.

 words.find( "test" );

 words.count( "test" );  // same key is unique in map, otherwise is multimap.


6. Set
    Every element is Key.
    Same key is unique in set, otherwise is multiset.
    Set sort by less than in default.

7. Iterator Inserters  // Insertion adapters
   <1> back_inserter()
   <2> inserter()
   <3> front_inserter()

8. iostream iterators

   e.g.
 #include <iostream>
 #include <iterator>
 #include <algorithm>
 #include <vector>
 #include <string>

 using namespace std;
 
 int main()
 {
     ifstream in_file( "input_file.txt" );
     ofstream out_file( "output_file.txt" );     
    
     if( !in_file || !out_file )
     {
         cerr << "!!unable to open the necessary file!/n";
         return -1;
     }
    
     istream_iterator< string > is( cin );
     //istream_iterator< string > is( in_file );
     istream_iterator< string > eof;
    
    
     vector< string > text;
    
     copy( is, eof, back_inserter( text ) );
    
     sort( text.begin(), text.end() );
    
     ostream_iterator< string > os( cout, " " );
     //ostream_iterator< string > os( out_file, " " );
     copy( text.begin(), text.end(), os );
 }
 
<< PART 4 >> // based on object

1. Constructor and Destructor
 <a>. Constructor has no return value and function type, but it could be overload !

 <b>. Member initializtion list

 <c>. Destructor has no return value and parameter could not be overload !


2. mutable and const

    Rule 1: const reference class could not invoke the non-const member by public interface !


3. static class member
  

4. Iterator class
 ???

5. friend  

6. copy constructor , copy assignment operator

7. function object
   "function object" is a "提供有 function call 运算符"的class

8. // source page is 4.11
   pointer to member function

   void (num_sequence:: *pm)( int ) = NULL;

   static vector< vector<int> > seq;
   static vector<vector<int>> seq; is error! // lost white space
   相当于2维,为何不用 vector<int> seq[7][8];   ???

  
   注意指向class成员函数的函数指针的用法,好像比较繁琐!
 
   typedef void (num_sequence:: *PtrType)( int );
  
   num_sequence ns;
   num_sequence *pns = &ns;
   PtrType pm = &num_sequence::fibonacci;
  
   用法:
   (ns.*pm)( pos );
   (pns->*pm)( pos );
  
   Could I use this ???
   PtrType pm = &(pns->fibonacci);
 
   pm( pos ); 
  
  

<< PART 5 >> Object-oriented Programming

1. Key words
   inheritance , ploymorphism , dynamic binding
 
2. define a Abstract Base Class
   <1> 找出所有子类共通的操作行为;
   <2> 找出哪些操作行为与型别相关(tpye dependent), will be virtual functions;
   <3> 确定每个操作行为的access level( public, protected, private );

   Concept:
   <1> pure virtual functions --> abstract base class
   <2> 当base class 有一个or多个虚函数,一般应该将其destructor 声明成virtual;
   <3> component of Derived calss;
   <4> 当Derived calss 中的 member和 Based class 中的member同名时,会遮盖住Based class 的member,
       除非使用 class scope 显示修饰;

3. 派生类中定义一个virtual function
   <1> 函数型别必须完全符合函数原型;
  
   ref to FAQ 6.


4. !!! static resolution of the virtual function !!!
   <1> 基类的 constructor and destructor 中调用 virtual function, will be static resolution;
 效果固定调用基类的函数!
 
   <2> 当使用基类的object, 而不是基类对象的pointer, reference时, be static resolution;
 声明对象,就配置出该对象的内存空间,传入的派生类对象多余部分被sliced off.

5. !!! Run-Time Type Identification !!!
   <1> #include <typeinfo>
   <2> typeid( pointer )
   <3> static_cast< pointer >
   <4> dynamic_cast< pointer >
   <5> const_cast< reference > // what, how ?

   ref to FAQ 7.
 

<< PART 6 >>  Programming with Templates
 
1. constructor 的参数化列表好像效率比在 constructor 内部赋值更高!
  
   <1> template<typename valtype>
       inline BTnode<valtype>::
       BTnode( const valtype &val )
  :_val( val ) // 只执行copy assignment operator
       {
           _cnt = 1;
       }

   <2> template<typename valtype>
       inline BTnode<valtype>::
       BTnode( const valtype &val )
       {
    _val = val; // val 的 constructor会先执行,再执行copy assignment operator
           _cnt = 1;
       }
   
     Method<2> is more effective then method<1>, when val is a class !
    

2. !!! binary tree's remove operation !!!


3. constant expressions and template parameters
   class template 的型别参数不仅仅可以传递元素类型,也可以 constant expressions !


<< PART 7 >>  Exception Handling

1. 任何一个被 throw的C++异常,都可以在程序某处找到相应的throw表达式,有些可能深入标准库.
   <1>  catch( ... ) // ... means catch all
 {
     throw ;
 }

2. try{}外的throw不被处理,但函数会被中断,由异常处理机制接管,沿函数调用链一路回溯,直到main()中还找不到合适的处理程序的话,调用系统的terminate()中断整个程序的执行.


3. 异常处理机制在终结某个函数之前,C++保证,函数中所有局部对象的destructor都会被调用!


4. BS 推荐资源作为局部对象来管理.

5. The Standard Exceptions
   Exceptions 类体系的好处: 可以被"打算捕捉抽象基类exception"的程序代码所捕捉!


//===================================================================================//

experience 1 : user class operator overload;

experience 2 : using const;

experience 3 : file scope is dangerous;

experience 4 : vector may be NULL, but array not. So judge vector.empty() would better;

experience 5 : vector.end() pointer to the address after last one;

experience 6 : type_of_object_pointed_to * name_of_pointer_object;

experience 7 : maximal munch RULE; e.g. a+++p equals a++ +p

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值