Page History
Table of Contents |
---|
Useful includes
Code Block |
---|
#include <stdlib.h>
#include <string> // for string, substring
#include <sstream> // for stringstream
#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
Code Block |
---|
double val = 12345.26374859560; std::stringstream ss; ss << std::setw(8) << std::setprecision(8) << std::right << val; std::string s = ss.str(); |
Input from stringstream
Code Block |
---|
std::stringstream ss(s); T val; do { ss >> val; cout << val << endl; } while( ss.good() ); |
Print long space or repeated character
...
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 <stdio.h> // 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",timeinfo);
cout << "strftime - user-formatted time char* : " << c_time_buf << endl;
printf ( "struct tm* members:\n");
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);
return ( EXIT_SUCCESS );
}
|
...
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 );
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);
|
Direct access to command line parameters in C++
Code Block |
---|
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "argc = " << argc << endl;
for(int i = 0; i < argc; i++)
cout << "argv[" << i << "] = " << argv[i] << endl;
return 0;
}
|
...
- The getopt() function shall return the next option character specified on the command line.
- A colon ( ':' ) shall be returned if getopt() detects a missing argument and the first character of optstring was a colon ( ':' ).
- A question mark ( '?' ) shall be returned if getopt() encounters an option character not in optstring or detects a missing argument and the first character of optstring was not a colon ( ':' ).
- Otherwise, getopt() shall return -1 when all command line options are parsed.
Code Block |
---|
#include <getopt.h>
#include <stdio.h>
#include <iostream>
//===================
void usage(char* name) {
std::cout << "Usage: " << name << " [-a <aaa>] [-b <bbb>] [-c <ccc>] [-h] <p1> [<p2> [<p3> ...]] \n";
}
//===================
int main (int argc, char **argv)
{
char *avalue = NULL;
char *bvalue = NULL;
char *cvalue = NULL;
int index;
int c;
extern char *optarg;
extern int optind, optopt, opterr;
while ((c = getopt (argc, argv, ":a:b:c:h")) != -1)
switch (c)
{
case 'a':
avalue = optarg;
printf ("a: avalue = %s\n",avalue);
break;
case 'b':
bvalue = optarg;
printf ("b: bvalue = %s\n",bvalue);
break;
case 'c':
cvalue = optarg;
printf ("c: cvalue = %s\n",cvalue);
break;
case 'h':
printf ("h: ");
usage(argv[0]);
break;
case ':':
printf ("(:) Option -%c requires an argument.\n", optopt);
usage(argv[0]);
return EXIT_FAILURE;
case '?':
printf ("?: Option -%c requires an argument.\n", optopt);
usage(argv[0]);
return EXIT_FAILURE;
default:
printf ("default: You should not get here... option -%c .\n", optopt);
abort ();
}
printf ("End of options: avalue = %s, bvalue = %s, cvalue = %s\n",
avalue, bvalue, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return EXIT_SUCCESS;
}
|
...
Using opendir
, readdir
, closedir
from STL
Code Block |
---|
#include <iostream>
#include <dirent.h>
using namespace std;
int main() {
DIR* dir;
dirent* pdir;
dir = opendir("."); // open current directory
while (pdir = readdir(dir)) {
cout << pdir->d_name << endl;
}
closedir(dir);
return 0;
}
|
Using boost
Code Block |
---|
#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
Code Block |
---|
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
|
...
InputParameters.h :
Code Block |
---|
#include <iostream> // for cout
class InputParameters {
public:
static InputParameters* instance();
void print();
private:
InputParameters () ; // !!!!! Private so that it can not be called from outside
virtual ~InputParameters () {};
static InputParameters* m_pInstance; // !!!!! Singleton instance
// Copy constructor and assignment are disabled by default
InputParameters ( const InputParameters& ) ;
InputParameters& operator = ( const InputParameters& ) ;
};
|
InputParameters.cc :
Code Block |
---|
#include "<Package>/InputParameters.h"
InputParameters* InputParameters::m_pInstance = NULL; // !!!!!! make global pointer !!!!!
//----------------
InputParameters::InputParameters ()
{
std::cout << "!!!!!!!! Single instance for singleton class InputParameters is created \n";
}
//----------------
InputParameters* InputParameters::instance()
{
if( !m_pInstance ) m_pInstance = new InputParameters();
return m_pInstance;
}
//----------------
void InputParameters::print() { std::cout << "InputParameters::print()\n"; }
|
Calls:
Code Block |
---|
#include "<Package>/InputParameters.h"
int main(int argc, char *argv[])
{
InputParameters::instance()->print("xyz...");
}
|
Shortcuts:
Code Block |
---|
#define PRINT InputParameters::instance()->print
int main(){
PRINT("This is a line of log");
return 0;
}
|
Overview
Content Tools