Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TListAndSTL.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_cont_legacy
3/// This is an example of using TList with STL algoritms in CLING.
4///
5/// #### Output produced by `.x TListAndSTL.C`
6/// \macro_output
7///
8/// #### `TListAndSTL.C` code
9/// \macro_code
10///
11/// \author Anar Manafov
12
13// STD
14#include <algorithm>
15#include <iostream>
16#include <sstream>
17
18// ROOT
19#include "TList.h"
20#include "TCollection.h"
21#include "TObjString.h"
22
23// A functor for the for_each algorithm
24struct SEnumFunctor {
25 bool operator()(TObject *aObj) {
26 if (!aObj)
27 return false;
28
29 TObjString *str(dynamic_cast<TObjString*>(aObj));
30 if (!str)
31 return false;
32
33 cout << "Value: " << str->String().Data() << endl;
34 return true;
35 }
36};
37
38// A functor for the find_if algorithm
39struct SFind {
40 // using this ugly constructor, since there is problems with std::bindX in CINT
41
42 SFind(const TString &aStr): fToFind(aStr) {
43
44 }
45 bool operator()(TObject *aObj) {
46 TObjString *str(dynamic_cast<TObjString*>(aObj));
47 return !str->String().CompareTo(fToFind);
48 }
49private:
50 const TString fToFind;
51};
52
53
54// The "main" function
55void TListAndSTL()
56{
57 const Int_t size(10);
58
59 // Initializing TList container
61 ostringstream ss;
62 for (int i = 0; i < size; ++i) {
63 ss << "test string #" << i;
64 TObjString *s(new TObjString(ss.str().c_str()));
65 stringList.Add(s);
66 ss.str("");
67 }
68
69
70 // ### Example #1
71 // Running the std::for_each algorithm on the list
72 for_each(stringList.begin(), stringList.end(), SEnumFunctor());
73
74 // ### Example #2
75 // We can try to find something in the container
76 // using the std::find_if algorithm on the list
77 string strToFind("test string #4");
78 SFind func(strToFind.c_str());
79
82 = find_if(iter_cat.Begin(), TIterCategory<TList>::End(), func);
83
84 // Checking the result
85 if (!(*found)) {
86 cerr << "Can't find the string: \"" << strToFind << "\" in the container" << endl;
87 return;
88 }
89
90 TObjString *str(dynamic_cast<TObjString*>(*found));
91 if (!str) {
92 cerr << "Can't find the string: \"" << strToFind << "\" in the container" << endl;
93 return;
94 }
95
96 cout << "The string has been found: " << str->String().Data() << endl;
97}
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