Code written in C++

Timer

Posted by Jorden Mauro about 1 year ago
Timer class. Starts counting when an object is instantiated, but can be reset via a call to start_timer(). Elapsed time is extracted simply by calling the object name as if it were a function (operator())
class Timer{
public:
   Timer() { start=clock(); }
   ~Timer() {}
   void start_timer() { start=clock(); }
   double operator()() const
   {
      return (static_cast(clock()-start) / 
	      static_cast(CLOCKS_PER_SEC));
   }
private:
   clock_t start;
};
Language C++ / Tagged with timer, function object

Print Container

Posted by Jorden Mauro about 1 year ago
Prints the contents of a container to the console.
template void printContainer(const C& c)
{
   typename C::const_iterator it(c.begin());
   while (it!=c.end()-1){
      std::cout<<*it++<<", ";
   }
   std::cout<<*it;
}
Language C++ / Tagged with print, container

Convert to string

Posted by Jorden Mauro about 1 year ago
In case you don't have boost and need to convert a POD to string:
template inline std::string makeString(const T& val)
{
   std::ostringstream os;
   os<
Language C++ / Tagged with string, convert

Delay Function

Posted by Jorden Mauro about 1 year ago
Does just what it says...
inline void delay(const double& d)
{    
   clock_t start=clock();
   while( (clock()-start)
Language C++ / Tagged with delay

String replacement

Posted by Chad Humphries over 2 years ago
Never thought i would put something from PHP into C++.

// Returns a string with all occurrences of search in subject replaced with the given replace string.

std::string str_replace(const std::string& search, const std::string& replace, std::string subject)
{
	int pos = subject.find(search, 0);
	while(pos != std::string::npos)
	{
		subject.erase(pos, search.length());
		subject.insert(pos, replace);
		pos = subject.find(search, pos + replace.length());
	}
	return subject;
}
Language C++ / Tagged with strings