夏天/isummer

Sun of my life !Talk is cheap, Show me the code! 追风赶月莫停留,平芜尽处是春山~

博客园 首页 新随笔 联系 管理

 

1. class的virtual 与non-virtual的区别

(1)virtual 函数时动态绑定,而non-virtual是静态绑定,前者是多态效果。

(2)多态类的析构函数应该为virtual函数。

2. #define后面的"\"

  #define后面的"\"是续行符,表示下面一行是紧接着当前行的,一般用于将十分长的代码语句分几段写(语句本身要求必须是一行)。 这段代码就和下面的一样。
  但要注意\后面除了换行回车不能有任何字符,空格也不行:

#define EQN_TEST_BULK(EXPR, R1, R2, R3, R4, PASS) \
        { \
          double res[] = { R1, R2, R3, R4 }; \
          iStat += EqnTestBulk(_T(EXPR), res, (PASS)); \
        }

 3.  string类中的find_last_of()函数可以执行简单的模式匹配

  如在字符串中查找单个字符c,而且该函数是查找字符串的最后出现c的位置,如果无匹配就返回-1.这里的found=str.find_last_of("/\\");就是在字符串str中查找最后出现斜杠“/”或反斜杠“\”的位置,这一般在一个完整的路径中为了得到文件名等信息做准备。

 1 // string::find_last_of
 2 #include <iostream>       // std::cout
 3 #include <string>         // std::string
 4 #inlude <cstddef>         // std::size_t
 5 
 6 void SplitFilename (const std::string& str)
 7 {
 8   std::cout << "Splitting: " << str << '\n';
 9   std::size_t found = str.find_last_of("/\\");
10   std::cout << " path: " << str.substr(0,found) << '\n';
11   std::cout << " file: " << str.substr(found+1) << '\n';
12 }
13 
14 int main ()
15 {
16   std::string str1 ("/usr/bin/man");
17   std::string str2 ("c:\\windows\\winhelp.exe");
18 
19   SplitFilename (str1);
20   SplitFilename (str2);
21 
22   return 0;
23 }

  输出

Splitting: /usr/bin/man
 path: /usr/bin
 file: man
Splitting: c:\windows\winhelp.exe
 path: c:\windows
 file: winhelp.exe

 

posted on 2016-01-10 21:48  夏天/isummer  阅读(386)  评论(0编辑  收藏  举报