Skip to content

Instantly share code, notes, and snippets.

@xaratustrah
Last active August 27, 2018 12:07
Show Gist options
  • Save xaratustrah/8d1379b6ac75aa086d4a to your computer and use it in GitHub Desktop.
Save xaratustrah/8d1379b6ac75aa086d4a to your computer and use it in GitHub Desktop.
c_cpp_tricks.md

C/C++ tips & tricks

Pair in C++

#include <utility>
#include <iostream>
#include <math.h>
 
std::pair<double, double> cart2pol(double real, double imag)
{
     std::pair<double, double> result;
     result = std::make_pair(sqrt(pow(real, 2)+pow(imag, 2)), atan(imag/real));
     return result;
}
 
int main()
{
     std::cout << "Mag: " << cart2pol(1, 1).first << std::endl << "Phase: " <<  cart2pol(1, 1).second << std::endl;
}

Returning a pointer

#include <iostream>
// test the funny warning from compiler

double * assign ()
{
//     double out[2];  // works but causes the famous error
     double* out = (double *)malloc(2 * sizeof(double));
     out[0] = 0;
     out[1] = 1;
     return out;
}

int main ()
{
     double *in = 0;
     in = assign();
     double first = in[0];
     double second = in[1];
     std::cout << first << " and " << second << std::endl;
     free(in);
     return 0;
}

Static variables

Klasse variable = Klasse (parameter);
Klasse variable = parameter;
Klasse variable (parameter);

Dynamic variables

Klasse *variable = new Klasse (parameter);
delete variable;

Dynamic array

Klasse * field = new Klasse [128];
delete [] field;

Array of Pointers and calling constructors with parameters

MyClass **MyVar = new MyClass*[5];
for( ulong i=0; i


for( ulong i=0; i

Two dimentional array

int x,y;
int **a = new int*[x];
for (int i = 0; i

another example

double **pp;
  int n = 3,m=4;

  // Allocation
  pp = new double* [n];
  for(register int i=0; i=0; i--)delete [] pp[i];
  delete [] pp;

another example

// allocation
double** pp = new double* [n];
pp[0] = new double [n*m];
for (int i = 1; i

Pointers

// declaration
int *p; // pointer to an int value
int i; // a variable with integer value

// usage

p = &amp;i; // p will contain the address of i
i = *p; // i will contain the value pointed to by p

Usage of Const with pointers

const Object* obj; // can't change data
Object const *obj; // same as const Object* obj;
Object* const obj; // can't change pointer
const Object* const obj; // can't change data or pointer

TNtuples in ROOT

// Reads a file and throws out an n-tuple
TNtuple * create_ntuple_from_file (const char* filename){

    ifstream in;
    in.open(filename);
    Float_t x,y,df;
    Int_t nlines = 0;
    TNtuple *ntuple = new TNtuple("ntuple","x, y and  Frequency","x,y,df");
    while (1) {
        in &gt;&gt; x &gt;&gt; y &gt;&gt; df;
        if (!in.good()) break; ntuple-&gt;Fill(x,y,df); nlines++;
    }
    cout ReadFile(filename, "freq:ampldb");
    cout Draw("ampl:freq&gt;&gt;h1","","cont4");
    h1-&gt;GetXaxis()-&gt;SetTitle("Frequency");
    h1-&gt;GetYaxis()-&gt;SetTitle("Amplitude");
    c1-&gt;Update();
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment