Since a couple of days I research for a method to determine the number of clusters for K-means automatically, I found elbow method but I can not till now understand its principle.

Is there any algorithm or C++ code of elbow method or other simple method to determine the number of classes automatically