Page History
...
std::cout << std::setw(80) << " " << std::setw(4);
Check if the element is 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()) {/*element is in v*/} else {/* is not there */} |
Add the element to the vector or list if it is not there
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<stdio.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 for printf #include <time.h> #include <iostream> // for cout using namespace std; int main () { // Get current time in seconds since January 1, 1970: time_t seconds; seconds = time (NULL); cout << seconds << " sec since January 1, 1970\n"; // Another form of the time access method: time_t time_sec; time ( &time_sec ); struct tm* timeinfo; timeinfo = localtime ( &time_sec ); cout << "time_sec = " << time_sec << endl; cout << "ctime - converts time_sec in char* : " << ctime (&time_sec); char* p_buf = asctime(timeinfo); cout << "asctime - converts struct tm* in char*: " << p_buf; char c_time_buf[80]; strftime (c_time_buf,80,"%Y-%m-%d %H:%M",timeinfo1timeinfo); cout << "test strftimestrftime - user-formatted time char* : " << 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 (NULLprintf ( "struct tm* members:\n"); printf ( "sec = %d\n", timeinfo->tm_sec ); printf ( "\n%ldmin seconds since January 1, 1970= %d\n", seconds); // Another method: time_t rawtime; time ( &rawtime timeinfo->tm_min ); printf ( "rawtimehour = %ld. The current local time is: %s", rawtime, ctime (&rawtime)%d\n", timeinfo->tm_hour ); structprintf tm( * timeinfo; timeinfo = localtime ( &rawtime"mday = %d\n", timeinfo->tm_mday ); printf ( "Themon current date/time is: %s= %d\n", asctime (timeinfo)timeinfo->tm_mon ); printf ( "secyear = %d\n", timeinfo->tm_sec year ); printf ( "min wday = %d\n", timeinfo->tm_minwday ); printf ( "houryday = %d\n", timeinfo->tm_houryday ); printf ( "mday isdst= %d\n", timeinfo->tm_mday ); printf ( "mon = %d\n",isdst); return ( EXIT_SUCCESS ); } |
Precise time
Use another time structure, which accounts for sec and nsec.
Code Block |
---|
#include <time.h> // time struct timespec start, stop; int gettimeStatus = clock_gettime( CLOCK_REALTIME, &start ); timeinfo->tm_mon ); printf ( "yeargettimeStatus: =%d %d\n", timeinfo->tm_yeargettimeStatus ); printf ( "wdaystart.tv_sec = %d start.tv_nsec = %d \n", timeinfo->tm_wdaystart.tv_sec, start.tv_nsec ); gettimeStatus = clock_gettime( CLOCK_REALTIME, &stop ); printf ( "yday stop.tv_sec = %d stop.tv_nsec = %d \n", stop.tv_sec, timeinfo->tm_yday stop.tv_nsec ); printf ( "isdst dt_sec = %d dt_nsec = %d \n", timeinfo->tm_isdststop.tv_sec - start.tv_sec, stop.tv_nsec- start.tv_nsec); |
Using boost
Iterate over files in the directory
...
Overview
Content Tools