C/C++ Tips

  1. 如果struct只给了A(int i)这种函数,没给A(),则不能用A* p1 = new A;来新建,需用A* p1 = new A(1);来弄。
  2.  

  3. find判断没有找到substring时不能用 !str.find(substr);, 需用 str.find(substr) == -1; 否则会有错。
  4.  

  5. C++最简单的将任意格式x转换成string的方法(记住用完一定要 ss.clear();,不然会有奇怪的bug,目测是因为“\n, 空格, \r” 等符号残留导致的):
  6. 1
    2
    3
    4
    stringstream ss;
    ss << x;
    string str;
    ss >> str;

     

  7. 32 bit integer 上下界: y>=2147483647 || y <= -2147483648 或者 Integer.MAX_VALUE || rev < Integer.MIN_VALUE
  8.  

  9. 不管INT_MIN 还是 -INT_MIN; 得到的都是负数(这是有由于下界的绝对值大于上界限导致的),实例如下:
  10. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #include <iostream>
    int main() {
        std::cout << INT_MIN << std::endl;
        std::cout << -INT_MIN << std::endl;
        return 0;
    /*
    -2147483648
    -2147483648
    */

    }

    需要算完之后再取负值才可以。

    1
    2
    int rmd = INT_MIN % 10;
    cout<<-rmd<<endl;

    才能拿到正数。

     

  11. ++i  与 i++:
  12. ++i will increment the value of i, and then return the incremented value.

    1
    2
    3
    i = 1;
    j = ++i;
    //(i is 2, j is 2)

    i++ will increment the value of i, but return the original value that i held before being incremented.

    1
    2
    3
    i = 1;
    j = i++;
    //(i is 2, j is 1)

     

  13. Some Tips about types:
    1. Use an unsigned type when you know that the values cannot be negative.
    2. If your data values are larger than the minimum guaranteed size of an int, then use long long.
    3. Use double for floating-point computations; float usually does not have enough precision but the cost is nearly the same.
    4. Assign bool from other types is tricky:
      1
      2
      3
      4
      int i = -1;
      bool temp = i++;//temp is true!
      temp =//temp is false
      // bool is false only when the value is 0!

     

  14. vector.size() returns size_t,如果确信size不会超过INT_MAX则可以忽略掉warning。建议使用以下方式而非size来循环:
    1
    for( auto it = v.begin(); it != v.end(); it++ )

     

  15. extern is used when you initialize the variable in other files. Simply speaking, to use the same variable (like global variable) in multiple files, we must define that variable in one—and only one—file. Other files that use that variable must declare—but not define—that variable, which means we need to use extern.
  16.  

  17. Use nulllptr to replace NULL when assign a pointer, since nulllptr is always a pointer type while 0 (aka. C’s NULL bridged over into C++) could cause ambiguity in overloaded function resolution.
  18.  

  19. To share a const value among multiple files:
    1
    2
    //In the file for declaration and definition
    extern const int bufferSize = initBuffer();
    1
    2
    //In the file that needs this variable(declaration only)
    extern const int bufferSize;
  20.  

  21. Difference between const pointer and pointer to const:
    1
    2
    3
    4
    5
    //pointer to const
    int i = 1;
    const int* ptr = &i;
    *ptr = 1; //illegal, since ptr think it points to a const value
    i = 2;    //legal
    1
    2
    3
    4
    5
    6
    //const pointer
    int i = 1;
    int i2 = 0;
    int* const ptr = &i;
    *ptr = 1; //legal
    ptr = &i2;  //illegal, since ptr always points to i and cannot be changed.

发表评论

电子邮件地址不会被公开。 必填项已用*标注