This is an example of using TList with STL algoritms in CLING.
.x TListAndSTL.C
¶TListAndSTL.C
code¶Author: Anar Manafov
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, April 17, 2024 at 11:14 AM.
%%cpp -d
STD
%%cpp -d
#include <algorithm>
#include <iostream>
#include <sstream>
ROOT
%%cpp -d
#include "TList.h"
#include "TCollection.h"
#include "TObjString.h"
A functor for the for_each algorithm
struct SEnumFunctor {
bool operator()(TObject *aObj) {
if (!aObj)
return false;
TObjString *str(dynamic_cast<TObjString*>(aObj));
if (!str)
return false;
cout << "Value: " << str->String().Data() << endl;
return true;
}
};
%%cpp -d
input_line_45:15:2: error: expected expression %%cpp -d ^ input_line_45:15:3: error: expected expression %%cpp -d ^ input_line_45:15:4: error: use of undeclared identifier 'cpp' %%cpp -d ^ input_line_45:15:9: error: use of undeclared identifier 'd' %%cpp -d ^
A functor for the find_if algorithm
struct SFind {
// using this ugly constructor, since there is problems with std::bindX in CINT
SFind(const TString &aStr): fToFind(aStr) {
}
bool operator()(TObject *aObj) {
TObjString *str(dynamic_cast<TObjString*>(aObj));
return !str->String().CompareTo(fToFind);
}
private:
const TString fToFind;
};
const Int_t size(10);
input_line_55:2:2: warning: 'size' shadows a declaration with the same name in the 'std' namespace; use '::size' to reference this declaration const Int_t size(10); ^
Initializing TList container
TList stringList;
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("");
}
input_line_56:4:21: error: reference to 'size' is ambiguous for (int i = 0; i < size; ++i) { ^ input_line_55:2:14: note: candidate found by name lookup is 'size' const Int_t size(10); ^ /usr/include/c++/9/bits/range_access.h:252:5: note: candidate found by name lookup is 'std::size' size(const _Tp (&/*__array*/)[_Nm]) noexcept ^ /usr/include/c++/9/bits/range_access.h:242:5: note: candidate found by name lookup is 'std::size' size(const _Container& __cont) noexcept(noexcept(__cont.size())) ^ input_line_56:4:21: error: reference to 'size' is ambiguous for (int i = 0; i < size; ++i) { ^ input_line_55:2:14: note: candidate found by name lookup is 'size' const Int_t size(10); ^ /usr/include/c++/9/bits/range_access.h:252:5: note: candidate found by name lookup is 'std::size' size(const _Tp (&/*__array*/)[_Nm]) noexcept ^ /usr/include/c++/9/bits/range_access.h:242:5: note: candidate found by name lookup is 'std::size' size(const _Container& __cont) noexcept(noexcept(__cont.size())) ^
Running the std::for_each algorithm on the list
for_each(stringList.begin(), stringList.end(), SEnumFunctor());
input_line_58:2:12: error: use of undeclared identifier 'stringList' (for_each(stringList.begin(), stringList.end(), SEnumFunctor())) ^ input_line_58:2:32: error: use of undeclared identifier 'stringList' (for_each(stringList.begin(), stringList.end(), SEnumFunctor())) ^ input_line_58:2:50: error: use of undeclared identifier 'SEnumFunctor' (for_each(stringList.begin(), stringList.end(), SEnumFunctor())) ^ Error in <HandleInterpreterException>: Error evaluating expression (for_each(stringList.begin(), stringList.end(), SEnumFunctor())) Execution of your code was aborted.
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());
TIterCategory<TList> iter_cat(&stringList);
TIterCategory<TList> found
= find_if(iter_cat.Begin(), TIterCategory<TList>::End(), func);
[runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN5TIterC2EP9TIterator, _ZN13TIterCategoryI5TListED0Ev, _ZN12__cling_N5215SFindC1ERKS0_, _ZN13TIterCategoryI5TListE5BeginEv, _ZN12__cling_N5215SFindC2EOS0_, _ZTISt17__iterator_traitsI9TListItervE, _ZN13TIterCategoryI5TListED2Ev, __cxx_global_var_initcling_module_334_.6, _ZN12__cling_N5215SFindC1EOS0_, _ZN12__cling_N52516__cling_Un1Qu324EPv, _ZNK5TItereqERKS_, _ZNKSt13__atomic_baseIhEcvhEv, _ZN12__cling_N5215SFindC1ERK7TString, _ZN12__cling_N5215SFindC2ERK7TString, _ZNSt13__atomic_baseIhEppEv, __cxx_global_var_initcling_module_334_, _ZN12__cling_N5215SFindC2ERKS0_, _ZNK5TIter11ShowMembersER16TMemberInspector, _ZSt9__find_ifI13TIterCategoryI5TListEN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEEET_S9_S9_T0_St18input_iterator_tag, _ZN10TObjString6StringEv, _ZN12__cling_N5215SFindD2Ev, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEclI13TIterCategoryI5TListEEEbT_, _ZTS13TIterCategoryI5TListE, _GLOBAL__sub_I_cling_module_334, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEED2Ev, _ZN5TIterD2Ev, _ZN12__cling_N5215SFindclEP7TObject, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEC2ERKS4_, _ZSt19__iterator_categoryI13TIterCategoryI5TListEENSt15iterator_traitsIT_E17iterator_categoryERKS4_, _ZNSt13__atomic_baseIhEppEi, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEC1ES3_, _ZSt7find_ifI13TIterCategoryI5TListEN12__cling_N5215SFindEET_S5_S5_T0_, _ZStanSt12memory_orderSt23__memory_order_modifier, __cxx_global_var_initcling_module_334_.2, _ZN12__cling_N5258iter_catE, _ZN9__gnu_cxx5__ops11__pred_iterIN12__cling_N5215SFindEEENS0_10_Iter_predIT_EES5_, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEC2ES3_, _ZNK9TIteratoreqERKS_, _ZN5TIterppEv, __orc_init_func.cling-module-334, _ZN13TIterCategoryI5TListEC1ERKS1_, _ZTI13TIterCategoryI5TListE, _ZN12__cling_N5215SFindD1Ev, _ZTSSt17__iterator_traitsI9TListItervE, _ZTSSt15iterator_traitsI9TListIterE, $.cling-module-334.__inits.0, _ZN12__cling_N52524__dynamic__cling_Un1Qu30E, _ZNK5cling7runtime8internal15LifetimeHandler9getMemoryEv, _ZN12__cling_N5259strToFindB5cxx11E, _ZNK5TIter3IsAEv, _ZTV13TIterCategoryI5TListE, _ZNK5TIter27CheckTObjectHashConsistencyEv, _ZNK5TIterdeEv, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEC1ERKS4_, _ZN13TIterCategoryI5TListEC2EP9TIterator, _ZN13TIterCategoryI5TListEC2ERKS1_, _ZSt9__find_ifI13TIterCategoryI5TListEN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEEEET_S9_S9_T0_, _ZTISt15iterator_traitsI9TListIterE, _ZN13TIterCategoryI5TListED1Ev, __cxx_global_var_initcling_module_334_.1, _ZN12__cling_N5255foundE, _ZN12__cling_N5254funcE, _ZN13TIterCategoryI5TListEC1EP9TIterator, _ZNK5TIterneERKS_, _ZN13TIterCategoryI5TListE3EndEv, __cxx_global_var_initcling_module_334_.5, _ZN9__gnu_cxx5__ops10_Iter_predIN12__cling_N5215SFindEED1Ev }) } IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerC1EPNS1_15DynamicExprInfoEPN5clang11DeclContextEPKcPNS_11InterpreterE' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::LifetimeHandler(cling::runtime::internal::DynamicExprInfo*, clang::DeclContext*, char const*, cling::Interpreter*) Maybe you need to load the corresponding shared library? IncrementalExecutor::executeFunction: symbol '_ZN5cling7runtime8internal15LifetimeHandlerD1Ev' unresolved while linking [cling interface function]! You are probably missing the definition of cling::runtime::internal::LifetimeHandler::~LifetimeHandler() Maybe you need to load the corresponding shared library?
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;
cling JIT session error: Failed to materialize symbols: { (main, { _ZN10TObjString6StringEv }) } [runStaticInitializersOnce]: Failed to materialize symbols: { (main, { _ZN12__cling_N5263strE, __vd_init_order__cling_Un1Qu31, _ZN12__cling_N52616__cling_Un1Qu325EPv, __cxx_global_var_initcling_module_335_, __cxx_global_var_initcling_module_335_.2, _GLOBAL__sub_I_cling_module_335, $.cling-module-335.__inits.0, _Z30__fd_init_order__cling_Un1Qu30v, __orc_init_func.cling-module-335 }) }