C++ 编程规则 使用范例

栏目: C++ · 发布时间: 6年前

内容简介:头文件的顺序.1. 系统/基本头文件2. 其他工程头文件

头文件的顺序. 

1. 系统/基本头文件

2. 其他工程头文件

3. 本工程头文件

4. 本类头文件.

类定义中的成员变脸和成员函数排列顺序

1. 枚举/结构体, 固定使用public类型, 排在最前

2. private 成员 变量

3. protected 成员变量

4. public 成员变量

5. private  成员函数

6. protected  成员 函数

7. public 

成员 函数

细分顺序

本类const 成员 函数

本类非const 成员 函数

本类const虚 函数

本类非const 函数

基类const 成员 函数

类非const 成员 函数    

点击( 此处 )折叠或打开  迭代器的使用范例.

  1. # include < iostream >
  2. # include < map >
  3. using namespace std ;
  4. class CMapExa
  5. {
  6. public :
  7.   void OnInit ( ) ;
  8.   void DelFunConfuse ( ) ;
  9.   void DelFunCorrect ( ) ;
  10.   void Show ( ) ;
  11. private :
  12.    typedef map < int , int > MAP_DATA ;
  13.   MAP_DATA m_map ;
  14. } ;
  15. void CMapExa : : OnInit ( )
  16. {
  17.   m_map . insert ( MAP_DATA : : value_type ( 1 , 1 ) ) ;
  18.   m_map . insert ( MAP_DATA : : value_type ( 2 , 2 ) ) ;
  19.   m_map . insert ( MAP_DATA : : value_type ( 3 , 3 ) ) ;
  20. }
  21. void CMapExa : : DelFunConfuse ( ) //不好的方式
  22. {
  23.    for ( MAP_DATA : : iterator it = m_map . begin ( ) ; it ! = m_map . end ( ) ; )
  24.   {
  25.     m_map . erase ( it + + ) ; //容易错误使用为 m_map . erase ( it ) ;
  26.   }
  27. }
  28. void CMapExa : : DelFunCorrect ( ) //建议的方式
  29. {
  30.   MAP_DATA delMap ;
  31.    for ( MAP_DATA : : iterator it = m_map . begin ( ) ; it ! = m_map . end ( ) ; + + it ) {
  32.     delMap [ it - > first ] = it - > second ;
  33.   }
  34.    for ( MAP_DATA : : iterator it = delMap . begin ( ) ; it ! = delMap . end ( ) ; + + it ) {
  35.     MAP_DATA : : iterator itDel = m_map . find ( it - > first ) ;
  36.      if ( itDel ! = m_map . end ( ) )
  37.       m_map . erase ( itDel ) ;
  38.   }
  39.   delMap . clear ( ) ;
  40. }
  41. void CMapExa : : Show ( )
  42. {
  43.    for ( MAP_DATA : : iterator it = m_map . begin ( ) ; it ! = m_map . end ( ) ; it + + )
  44.   {
  45.      cout < < it - > first < < endl ;
  46.      cout < < it - > second < < endl ;
  47.   }
  48. }
  49. int main ( )
  50. {
  51.   CMapExa objMapExa ;
  52.   objMapExa . OnInit ( ) ;
  53.   objMapExa . DelFunCorrect ( ) ;
  54.   //objMapExa . DelFunConfuse ( ) ;
  55.   objMapExa . Show ( ) ;

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Science of Programming

The Science of Programming

David Gries / Springer / 1989-4-21 / USD 99.00

Describes basic programming principles and their step-by- step applications.Numerous examples are included.一起来看看 《The Science of Programming》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具