• My Trap For C++


    1 My Trap For C++

    Discovery my fauls in my habit and overcome them.

    1.1 Initialize variable

    In 2012, at about 1:00am, I still worked in company, suddenly other staff discovevied crashes in server. I checked it, crashes occured when container's iterator isn't initialized; because of mass copying-code, the bugs appears in many places. I rapidly completed all the variable initializaiton.

    From then on, when a variable is declared, I must initialize it.

    Gernerally, we initialize different types as below:

    general type

    int a = 0;
    double a = 0.0;
    char a = '';
    

    general type's array

    int a[10] = {0};
    double f[10] = {0.0};
    

    pointer

    int* p_a = NULL;
    std::vector<int>* p_a = NULL;
    

    class

    class Object {
     public:
      Object():a(0), b(0.0), c('') {
      }
     private:
      int a;
      double b;
      char c;
    };
    

    Certainly you can initialize your variable with other value instead of default value above when your variable is not a undefilized variable.

    1.2 Remove when make iteration to container

    When making iteration to container, you are removing your container's elements.

    for (iter = c.begin(); c.end() != iter; ++iter) {
      if (xxx) {
        erase(iter);
      }
    }
    

    Obviously, you can find out the bug as soon as possible. When a function with removing the container's element is called in the "for" body, it isn't obvious for you.

    So when crashes appear during the iteration to container, you should check the "for" body carefully.

    I list the method of doing iteration when remove elements in the containers as below:

    vector, list

    std::vector<int>::iterator iter = v.begin();
    while (v.end() != iter) {
      if (xxx) {
        iter = v.erase(iter);
      } else {
        ++iter;
      }
    }
    

    map, set, multimap, multiset

    std::map<int>::iterator iter = rbtree.begin();
    while (rbtree.end() != iter) {
      if (xxx) {
        v.erase(iter++);
      } else {
        ++iter;
      }
    }
    

    To know all kind of the container's "erase" function, you can refer the website:

    http://www.cplusplus.com

    1.3 Forget increment or decrement when use "while"

    In the recent past, I use mass "while" without increment or decrement, make mass infinite loop. At that time, I lose the faith to myself. I replace "while" with "for" in my code as possible.

    for(std::hash_map<int, std::vector<int> >::iterator iter = m.begin(); m.end() != iter; ++iter) {
    //TODO
    }
    

    Obviousely "for" style reminds me to add increment or decrement. But "for" style's condition is too long, you can break the line and "while" style is cleaner. Now I can overcome the problem, I firstly implement a "while" frame with increment or decrement, then add my code in the "TODO" position.

    std::hash_map<int, std::vector<int> >::iterator iter = m.begin();
    while (m.end() != iter) {
      //TODO
      ++iter;
    }
    

    Tips: When add "continue" in the "TODO" position, after execute the "contine" command, "while" don't make indecrement or decrement, but "for" make it.

  • 相关阅读:
    【git】git命令集合
    【maven】【IDEA】idea中使用maven编译项目,报错java: 错误: 找不到符号 【2】
    【mybatis】mybatis传参的几种方式
    【EasyExcel】使用easyExcel过程中,项目报错的解决集合
    【Maven】【IDEA】在idea中开发web项目,解决maven的jar包冲突的方法
    【Easyexcel】java导入导出超大数据量的xlsx文件 解决方法
    【maven】idea的pom文件修改,引入新的jar包,无效,本地仓库始终没有下载新jar包的问题解决【idea pom Dependency not found】
    【java】javac编译多个有依赖关系的java文件为class文件
    【java】javac命令在win10不可用,提示javac不是内部或外部命令,也不是可运行的程序【解决方法】
    【POI】java服务生成List数据集合,后台服务生成xlsx临时文件,并将临时文件上传到腾讯云上,并实现xlsx浏览器预览
  • 原文地址:https://www.cnblogs.com/wind-qu/p/3800766.html
Copyright © 2020-2023  润新知