I found a nice link from Wikibooks where one can find many common algorithm implementations that occur in frequent tasks of programming:
Have a good time while exploring the contents!
inline vectorThis inline helper function splits a line of characters (split(string line, size_t minWordLength) { vector ret; size_t pos = 0; while (pos < line.size()) { while (pos < line.size() && !isalpha(line[pos])) pos++; size_t wordStart = pos; while (pos < line.size() && isalpha(line[pos])) pos++; size_t wordEnd = pos; if (wordEnd >= wordStart + minWordLength) { string word = line.substr(wordStart, wordEnd - wordStart); ret.push_back(word); } } return ret; }
string) and returns a list (vector) of words the line consists of. Given the minWordLength parameter only words with a minimum length of this size will be considered for the output.string and vector from the STL are required.
inline double startTime() {
struct timeval tp;
double sec, usec;
// Time stamp before the computations
gettimeofday(&tp, NULL);
sec = static_cast(tp.tv_sec);
usec = static_cast(tp.tv_usec) / 1E6;
return sec + usec;
}
inline double elapsedTime(double starttime) {
struct timeval tp;
double sec, usec;
// Time stamp after the computations
gettimeofday(&tp, NULL);
sec = static_cast(tp.tv_sec);
usec = static_cast(tp.tv_usec) / 1E6;
// Time calculation (in seconds)
return sec + usec - starttime;
}
A relatively easy way to measure time in C++ is to use standard linux libraries:
#include <sys/time.h> #include <sys/types.h>The first function (that can e.g. be global) returns the system time as double in microseconds. The second one computes the elapsed time according to the measurement given by the first function.
... double start = startTime(); // do something time consuming... double elapsed = elapsedTime(start); cout << "The operation took " << elapsed << "µs" << endl; ...