Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TListAndSTL.C File Reference

Detailed Description

This is an example of using TList with STL algoritms in CLING.

Output produced by .x TListAndSTL.C

Value: test string #0
Value: test string #1
Value: test string #2
Value: test string #3
Value: test string #4
Value: test string #5
Value: test string #6
Value: test string #7
Value: test string #8
Value: test string #9
The string has been found: test string #4

TListAndSTL.C code

// STD
#include <algorithm>
#include <iostream>
#include <sstream>
// ROOT
#include "TList.h"
#include "TCollection.h"
#include "TObjString.h"
// A functor for the for_each algorithm
struct SEnumFunctor {
if (!aObj)
return false;
TObjString *str(dynamic_cast<TObjString*>(aObj));
if (!str)
return false;
cout << "Value: " << str->String().Data() << endl;
return true;
}
};
// A functor for the find_if algorithm
struct SFind {
// using this ugly constructor, since there is problems with std::bindX in CINT
}
TObjString *str(dynamic_cast<TObjString*>(aObj));
return !str->String().CompareTo(fToFind);
}
private:
};
// The "main" function
{
const Int_t size(10);
// Initializing TList container
ostringstream ss;
for (int i = 0; i < size; ++i) {
ss << "test string #" << i;
TObjString *s(new TObjString(ss.str().c_str()));
stringList.Add(s);
ss.str("");
}
// ### Example #1
// Running the std::for_each algorithm on the list
// ### Example #2
// We can try to find something in the container
// using the std::find_if algorithm on the list
string strToFind("test string #4");
SFind func(strToFind.c_str());
// Checking the result
if (!(*found)) {
cerr << "Can't find the string: \"" << strToFind << "\" in the container" << endl;
return;
}
TObjString *str(dynamic_cast<TObjString*>(*found));
if (!str) {
cerr << "Can't find the string: \"" << strToFind << "\" in the container" << endl;
return;
}
cout << "The string has been found: " << str->String().Data() << endl;
}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
TRObject operator()(const T1 &t1) const
const_iterator begin() const
const_iterator end() const
A doubly linked list.
Definition TList.h:38
Collectable string class.
Definition TObjString.h:28
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:139
Author
Anar Manafov

Definition in file TListAndSTL.C.