Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Using STD lib

Useful includes

Code Block

#include <stdlib.h>
#include <string>   // for string, substring
#include <sstream>  // for streamstring
#include <iostream> // for cout, puts etc.
#include <stdio.h>  // for  sprintf, printf( "%lf\n", accum );
#include <time.h>   // time
#include <fcntl.h>  // open()
#include <unistd.h> // read()
#include <iomanip>  // for setw

using namespace std; // allows get rid of std:: prefix.

Formatting of the string

Formatting of the string

Code Block

double val = 12345.26374859560;
std::stringstream ss; ss << std::setw(8) << std::setprecision(8) << std::right << val; 
std::string s = ss.str();

Check if element in the vector or list

...

Code Block
  T element = <instatiation>;
  std::vector<T> v; v=<initialization>;
  if (std::find(v.begin(), v.end(), element) == v.end()) v.push_back(element);

Time

Code Block

#include <time.h>   // time

  struct timespec start, stop;
  int gettimeStatus = clock_gettime( CLOCK_REALTIME, &start );

  printf ( "gettimeStatus: %d \n", gettimeStatus );
  printf ( "start.tv_sec = %d  start.tv_nsec = %d \n", start.tv_sec, start.tv_nsec );

      gettimeStatus = clock_gettime( CLOCK_REALTIME, &stop );
  printf ( " stop.tv_sec = %d   stop.tv_nsec = %d \n", stop.tv_sec,  stop.tv_nsec );
  printf ( "      dt_sec = %d        dt_nsec = %d \n", stop.tv_sec - start.tv_sec, 
                                                       stop.tv_nsec- start.tv_nsec);

  struct tm * timeinfo1;   timeinfo1 = localtime ( &start.tv_sec ); 
  char   c_time_buf [80];  strftime (c_time_buf,80,"%Y-%m-%d %H:%M",timeinfo1);
  cout << "test strftime : " << c_time_buf << endl;  

  char *p_buf = asctime(timeinfo1);  
  cout << "test asctime : " << p_buf << endl;  

  // NULL gives seconds w.r.t. January 1, 1970:
  time_t seconds = time (NULL);
  printf ("\n%ld seconds since January 1, 1970\n", seconds);

  // Another method:
  time_t  rawtime;
  time ( &rawtime );
  printf ( "rawtime = %ld. The current local time is: %s", rawtime, ctime (&rawtime) );

  struct tm * timeinfo;
  timeinfo = localtime ( &rawtime );
  printf ( "The current date/time is: %s", asctime (timeinfo) );

  printf ( "sec  = %d\n", timeinfo->tm_sec  );
  printf ( "min  = %d\n", timeinfo->tm_min  );
  printf ( "hour = %d\n", timeinfo->tm_hour );
  printf ( "mday = %d\n", timeinfo->tm_mday );
  printf ( "mon  = %d\n", timeinfo->tm_mon  );
  printf ( "year = %d\n", timeinfo->tm_year );
  printf ( "wday = %d\n", timeinfo->tm_wday );
  printf ( "yday = %d\n", timeinfo->tm_yday );
  printf ( "isdst= %d\n", timeinfo->tm_isdst);

Using boost

Iterate over files in the directory

...