Hi ROOTERS,
Is there a standard function that returns the position(not value) of the element of an array of values?
For example:
say I have an array like this:
sampleArray = [1, 5, 2, 9, 4, 6, 3]
I want a function that returns the integer of 3 for element 9 in the array.
Thank you in advance.
Hi,
you can do this with standard stl. See e.g. cplusplus.com/reference/algorithm/find/
std::vector<int> v {1, 5, 2, 9, 4, 6, 3};
auto it = std::find(v.begin(),v.end(),9);
auto pos = std::distance(v.begin(), it);
Danilo
In Root, I encountered with following errors :
Error: Symbol v is not defined in current scope (tmpfile):1:
Error: Failed to evaluate v.begin()
Error: Symbol v is not defined in current scope (tmpfile):1:
Error: Failed to evaluate v.end()
Warning: Automatic variable it is allocated (tmpfile):1:
Error: Undeclared variable it (tmpfile):1:
*** Interpreter error recovered ***
Hi,
this works on ROOT6. Are you using ROOT5?
Danilo
Hi
My version of ROOT is 5.
Hi,
you have then to wrap those lines in a function and compile them with ACLiC:
int findIndex(const std::vector<int> v, const int x) {
auto it = std::find(v.begin(),v.end(),x);
if (it == v.end()) return -1;
return std::distance(v.begin(), it);
}
at the prompt
root [0] .L findIndex.C+
root [1] vector<int> v;v.push_back(4)
root [2] findIndex(v,4)
(int)0
root [3] findIndex(v,3)
(int)(-1)
the function returns -1 if the element is not present.
Note that there are a plethora of possible implementations for this function, this is one of the many.
Hi,
Error is the following:
root [1] .L findIndex.C
root [2] vector<int> v;v.push_back(4)
root [3] findIndex(v,4)
Error: Function distance(v.begin(),it) is not defined in current scope findIndex.C:12:
*** Interpreter error recovered ***
root [4] .L findIndex.C+
Info in <ACLiC>: script has already been loaded in interpreted mode
Info in <ACLiC>: unloading /home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/./findIndex.C and compiling it
Info in <TUnixSystem::ACLiC>: creating shared library /home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/./findIndex_C.so
In file included from /home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/findIndex_C_ACLiC_dict.h:34:0,
from /home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/findIndex_C_ACLiC_dict.cxx:17:
/home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/./findIndex.C: In function ‘int findIndex(std::vector<int>, int)’:
/home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/./findIndex.C:10:8: error: ‘it’ does not name a type
/home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/./findIndex.C:11:7: error: ‘it’ was not declared in this scope
/home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/./findIndex.C:12:35: error: ‘it’ was not declared in this scope
g++: error: /home/bayat/BINA/BINA_offline_ForThesis_elasticNtuple_Nov_2015_peds/macros/BallCalibMacros/findIndex_C_ACLiC_dict.o: No such file or directory
Error in <ACLiC>: Compilation failed!
Did you load the script befotr invoking aclic?
Note that it is required to compile root enabling c++11 for the auto keyword ( you can always use the vector iterator type explicitly nstead)