Using STD lib
Useful includes
#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
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
T element = <instatiation>; std::vector<T> v; v=<initialization>; if (std::find(v.begin(), v.end(), element) != v.end()) {/*element is in v*/} else {/* is not there */}
Add element to the vector or list if it is not there
T element = <instatiation>; std::vector<T> v; v=<initialization>; if (std::find(v.begin(), v.end(), element) == v.end()) v.push_back(element);
Time
#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
#include <boost/filesystem.hpp> namespace fs = boost::filesystem; fs::path path = "/reg/d/psdm/CXI/cxi49012/xtc/e158-r0150-s00-c01.xtc"; fs::path dir = path.parent_path(); // i.e.: /reg/d/psdm/CXI/cxi49012/xtc typedef fs::directory_iterator dir_iter; for (dir_iter dit = dir_iter(dir); dit != dir_iter(); ++ dit) { std::cout << dit->path().stem().string() << "\n"; }
Other methods of fs::path
fs::path path = m_path; std::cout << "stem = " << path.stem().string() << "\n"; // e158-r0150-s00-c00 std::cout << "extension() = " << path.extension().string() << "\n"; // .xtc std::cout << "filename() = " << path.filename().string() << "\n"; // e158-r0150-s00-c00.xtc std::cout << "parent_path() = " << path.parent_path().string() << "\n"; // /reg/d/psdm/CXI/cxi49012/xtc
Overview
Content Tools