zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Mysql 常用小技巧
    【JS学习】require('fs')(fs模块用于对系统文件及目录进行读写操作。)
    【JS学习】js中forEach与for循环
    【JS学习】ES6之async和await
    【JS学习】关于Vue.use()详解
    【Npm学习】npm run dev 和 npm run serve
    【Jenkins学习】修改插件下载源地址
    【JS学习】js中const,var,let区别
    【Go学习】知识分享之Golang——go mod时使用代理模式goproxy和私有模式GOPRIVATE
    【JS学习】export 和 export default 的区别
  • 原文地址:https://www.cnblogs.com/wind-qu/p/3800766.html
Copyright © 2011-2022 走看看