char peremen_t[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 

I need to output all the elements of the array. I understand that this requires a cycle:

 for (int i = 0; i < 10; i++) { qDebug() <<"peremen_t[i] " << peremen_t[i]; } 

And what if we add several elements, let's say:

 char peremen_t[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 101, 102, 103}; 

So how to sort through all these elements, constantly change the condition i < ... ? That's actually the question: how to sort through an array of elements, not knowing how many elements are there? :)

  • one
    количество элементов массива = sizeof(массива)/sizeof(одного элемента массива) . - post_zeew
  • Or there must be an obvious terminator in the array ... - Vladimir Martyanov
  • one
    Interestingly, the same question about java - αλεχολυτ
  • Yes, it was taken as a basis: 3, I hope the author of the question on jave is not too offended. He "Sashko" helped me a lot. - timob256

5 answers 5

There are several approaches. First, the size of the array can be calculated using the formula sizeof( массива ) / sizeof( элемента массива ) . for example

 for ( size_t i = 0; i < sizeof( peremen_t ) / sizeof( *peremen_t ); i++ ) { qDebug() <<"peremen_t[i] " << peremen_t[i]; } 

Secondly, you can use a for loop based on a range. If you need an element index, then you can define it before the loop. For example,

 size_t i = 0; for ( auto x : peremen_t ) { qDebug() <<"peremen_t[" << i << "] " << x; ++i; } 

You can also use a loop with iterators. For example,

 #include <iterator> // ... size_t i = 0; for ( auto it = std::begin( peremen_t ); it != std::end( peremen_t ); ++it, ++i ) { qDebug() <<"peremen_t[" << i << "] " << *it; } 

And finally, you can write a template function that performs the required task. For example,

 template <typename T, size_t N> inline void f( const T ( &peremen_t )[N] ) { for ( size_t i = 0; i < N; i++ ) { qDebug() <<"peremen_t[i] " << peremen_t[i]; } } 

In addition, you can use any limit value in the array. For example, for character arrays, this may be '\0' , provided that the actual elements of the array cannot contain this character, or some other unique value. For example,

 char peremen_t[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0 }; // ^^^ for ( size_t i = 0; permen_t[i] != '\0'; i++ ) { qDebug() <<"peremen_t[i] " << peremen_t[i]; } 
  • It is possible that in the standard with ++ 17 something has changed, but now you need to use _msize to determine the size (occupied memory) of the array by the pointer. - AlexIdest

Use modern C ++ (c ++ 11 and up):

 for( auto e : peremen_t ) { ... } 
  • The author of the item number displays in debug) so the number is getting ugly - pavel
  • @pavel to get the index, you can add a variable. But in general, it is certainly worthwhile to distinguish situations: only the values ​​are important, or else their index. range-for for situations where the index is not important. Actually, the author in general [i] is a string :) - αλεχολυτ
  • The @pavel element number in range-based for can be obtained through boost::adaptors::indexed . - Dev Null
 for( size_t i = 0; i < sizeof( peremen_t )/sizeof( peremen_t[0] ); i++ ) { /* .. */ } 
     ///функция для подсчётa количества символов в строке int counter(char In[]) { int i = 0; do{ i++; }while(In[i] != 0); return i; } 
    • one
      You can reduce the entire calculation to a single line for(;in[i]; ++i); - yrHeTaTeJlb
    • But yes, you are right. - Andrey Golubev
    • And here generally lines? char is not only a character, but also a number. - αλεχολυτ
    • one
      @yrHeTaTeJlb up to one line strlen . - αλεχολυτ

    On template functions and magic, ideone

     template <typename T, size_t N> size_t len(T (*)[N]) { return N; } 
    • std::size in c ++ 17 - Dev Null
    • @DevNull och witty, I take into account that the answer was written back in 2016) - user182055
    • and read the answer in 2018. in Russian so any other rules than in English? - Dev Null