Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Collections and Iterators in C++

Collections

  1. Prefer std::deque to std::vector when the size of the collection is unknow.

  2. Suppose set A and B are two set with the same type and set C is another set with the same value type but a different comparison function, then it is still valid to insert …

Boolean Values in C++

  1. Boolean expressions are evaluated from left to right (the same in Java), so it is totally OK to write code like

    if(a < x.size() && x[a]){
        ...
    }
    

    where x is a vector.

  2. There is no &&= and ||= operators in C++, instead you can use &= and |=. Though &= and |= are not specially for …

Probability to Lose All Money

A few days ago I found someone asking an interview questions on mitbbs. The question is as follows. A gambler plays a fair game and bet 1 dollar each time. If he lose all his money, the game stops. Suppose he has 10 dollars and is only allowed to play …

Working with Class in C++

Illustrative examples for the following discussions can be found here.

  1. It is suggested that you also provide a default constructor if you ever provide a user-defined constructor when writing a C++ class.

  2. If you want to allow deleting a derived class from a pointer of the base class, you have …