Back Midas Rome Roody Rootana
  Midas DAQ System  Not logged in ELOG logo
Entry  23 Jul 2025, Konstantin Olchanski, Suggestion, K.O.'s guide to new C/C++ data types 
    Reply  24 Jul 2025, Konstantin Olchanski, Suggestion, K.O.'s guide to new C/C++ data types 
Message ID: 3066     Entry time: 24 Jul 2025     In reply to: 3065
Author: Konstantin Olchanski 
Topic: Suggestion 
Subject: K.O.'s guide to new C/C++ data types 
> for (int i=0; i<array_of_10_elements.size(); i++)

becomes

for (size_t i=0; i<array.size(); i++)

but for a reverse loop, replacing "int" with "size_t" becomes a bug:

for (size_t i=array.size()-1; i>=0; i--)

explodes, last iteration should be with i set to 0, then i--
wraps it around a very big positive value and loop end condition
is still true (i>=0), the loop never ends. (why is there no GCC warning
that with "size_t i", "i>=0" is always true?

a kludge solution is:

for (size_t i=array.size()-1; ; i--) {
do_stuff(i, array[i]);
if (i==0) break;
}

if you do not need the index variable, you can use a reverse iterator (which is missing from a few 
container classes).

K.O.
ELOG V3.1.4-2e1708b5