How to determine the size of an array of strings in C++?

后端 未结 8 610
野的像风
野的像风 2020-12-30 06:57

I\'m trying to simply print out the values contained in an array.

I have an array of strings called \'result\'. I don\'t know exactly how big it is because it was au

相关标签:
8条回答
  • 2020-12-30 07:27

    As a side comment, there are better ways of checking the size of an array (for the cases where the array is in scope and has not decayed into a pointer) that are typesafe:

    // simple: runtime result
    template <typename T, std::size_t N>
    inline std::size_t sizeof_array( T (&)[N] ) {
       return N;
    }
    
    // complex: compile time constant
    template <typename T, std::size_t N>
    char (&static_sizeof_array( T(&)[N] ))[N];   // declared, not defined
    #defined SIZEOF_ARRAY( x ) sizeof(static_sizeof_array(x))
    

    In both cases the compiler will detect if you try to pass in a pointer (dynamic array or decayed array):

    void f( int array[] ) { // really: void f( int *array )
    {
    //   sizeof_array(array);              // compile time error
    //   int another[SIZEOF_ARRAY(array)]; // compile time error
    }
    int main() {
       int array[] = { 1, 2, 3 };
       std::cout << sizeof_array(array) << std::endl; // prints 3
       int another_array[ SIZEOF_ARRAY(array) ];
       std::cout << sizeof_array(another_array) << std::endl; // 3 again
    }
    
    0 讨论(0)
  • 2020-12-30 07:32

    Something to be aware of: text can be represented in different methods. An array of text can also be represented in different methods.

    Array of pointers to C-Style strings

    A common method is to have an array of pointers to char. The issue is that the size of the array doesn't represent the size of all of the text. Also, the ownership of the data or pointer must also be established, as the text may have to be delete (and can the callee delete the text or does the caller?). Because it is an array, the size of the array must always accompany the array in all parameters (unless the array is always a fixed size).

    Array of char - packed text

    Another method is to pass an array of char and have the strings contiguous in the array. One string follows the termination char of the previous. With this array, the total size of all of the strings is represented, no wasted space. Again, with arrays, the size of the array must accompany the array when passed around.

    Array of std::string

    In C++, text can be represented using std::string. In this case, the array represents the quantity of strings (similar to the array of C-Strings above). To get the total size of all the strings, one must sum up the size of each individual string. Since this is an array, the size of the array must be passed also.

    Summary

    During run-time array sizes must accompany the array when the array is passed around. sizeof is only processed at compile time. A simpler structure is std::vector, which handles size and memory allocation dynamically.

    0 讨论(0)
提交回复
热议问题