C++

The Intelligent Smoker

A person smokes cigarettes and leaves the lower portion (around 1/5) of it unburnt. He then combines the 5 residues of those cigarettes to create a new one for smoking.

If the person had initially 93 cigarettes, how many cigarettes did he actually smoked?

He will smoke 116 cigarettes.

Advanced STL

Sample code to explain some advanced STL concepts.

//
// STL.cpp
// Sample code to explain some advanced STL concepts. This is a shameless
// copy of online materials in order to create a simple example program to
// explain the basics of STL.
//
// Topics covered:
// ostream_iterator, copy, deque, insert_iterator, front_inserter,
// back_inserter, accumulate, count, count_if, find, find_if, generate,
// generate_n, fill, fill_n, transform, negate, mismatch, search, equal,
// for_each, swap, sort, merge, binary_search, includes, ptr_fun, set_union,

Singleton Design Pattern

Singleton Design Pattern is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Examples would include objects needed for logging, communication, database access, file manager, scheduler, etc. You could pass such an instance from method to method, or assign it to each object in the system. However, this adds a lot of unnecessary complexity. Simply put, singleton ensures that a class has only one instance, and provides a global point of access to it.

Programming Puzzle: Search Engine

The urban legend goes that if you go to the Google homepage and search for "Google", the universe will implode. We have a secret to share... It is true! Please don't try it, or tell anyone. All right, maybe not. We are just kidding. The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine's name, the universe does implode! To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine.

Programming Puzzle: Train Timetable

A train line has two stations on it, A and B. Trains can take trips from A to B or from B to A multiple times during a day. When a train arrives at B from A (or arrives at A from B), it needs a certain amount of time before it is ready to take the return journey - this is the turnaround time. For example, if a train arrives at
12:00 and the turnaround time is 0 minutes, it can leave immediately, at 12:00.

How to iterate vector in a loop and delete items from it?

You can't just iterate vector in a for loop and delete the item pointed by the iterator and increment that iterator at the end of the loop, as after deleting the item from the vector, the iterator is invalidated.

The crude way of deleting the items in a vector is:

typedef std::vector<std::string> string_vector;
typedef std::vector<std::string>::iterator string_vector_iterator;

string_vector_iterator iter = m_vPaths.begin();
while (iter != m_vPaths.end())
{
  if(::DeleteFile(iter->c_str()))
  {

How to disable warning in g++ in Linux?

When you embed an .xpm file to some C++ project in Linux, you will get tons of harmless warnings.

warning: deprecated conversion from string constant to ‘char*’

You can disable such warning in the project on global basis, by adding -Wno-write-strings to the g++ command line arguments.

How to reverse the order of the words in a sentence?

For example for a given string: "This is just a test", convert it to "test a just is This".

std::string reverse_words(const std::string &strInput)
{
    using namespace std;
    string result;
    string::const_reverse_iterator iterWordStart, iterWordEnd;
    bool bBufferMode = true;
    iterWordStart = iterWordEnd = strInput.rbegin();
    for(; iterWordStart != strInput.rend(); ++iterWordStart) {
        if(!(isalpha(*iterWordStart) && bBufferMode)) {
            reverse_copy(iterWordEnd, iterWordStart, inserter(result, result.end()));