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

后端 未结 8 609
野的像风
野的像风 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:14

    The sizeof(array)/sizeof(element) works for fixed-length-array of fixed-length-arrays (not of pointers). As an array of strings we most often use a (fixed-length-)array of pointers-to-various-(fixed-)length-strings so this trick wouldn't work. sizeof() is used for objects which size is known at compile time. It's not applicable to dynamically allocated data itself.

    When an object contains pointers like in the case of an array of strings, sizeof() returns the size of the highest-level (fixed-size) structure. Often it's just the size of a single pointer. It does not include the size of the allocated data pointed to by the pointers. Because that data actually is not part of the main object, it's indeed one or more separate objects (we have aggregation here instead of composition, see http://en.wikipedia.org/wiki/Object_composition).

    In C++ using vectors is very convenient for your needs. Other suitable standard containers could be used too. length() and size() methods are synonyms, see http://www.cplusplus.com/reference/string/string/size/)

    P.S. Please note that for std::string s object sizeof(s) is a constant independent of the actual (variable) string length returned by s.length(). The actual allocated memory size is returned by s.capacity() and could be greater than length().

    Example using vector array:

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        string s = "01234";
        cout << "s[" << s.length() << "]=\"" << s << "\"" << endl; 
        cout << "sizeof(s)=" << sizeof(s) << " (implementation dependent)" << endl;
        cout << endl;
    
        s += "56789012345";
        cout << "s[" << s.length() << "]=\"" << s << "\"" << endl; 
        cout << "sizeof(s)=" << sizeof(s) << " (implementation dependent)" << endl;
        cout << endl;
    
        vector<string>vs={"12","23","345","456","567","67888","7899999999","8","9876543210"};
    
        cout << "vs[" << vs.size() << "]={";
        size_t sz=0;
        for (size_t index=0; index<vs.size(); index++)
        {
            sz+=vs[index].size();
            if (index>0)
                cout << ",";
            cout << "\"" << vs[index] << "\":" << vs[index].size();
        }
        cout << "}:" << sz << endl;
        cout << "sizeof(vs)=" << sizeof(vs) << " (implementation dependent)" << endl;
    
        return 0;
    }
    

    Result:

    s[5]="01234"
    sizeof(s)=8 (implementation dependent)
    
    s[16]="0123456789012345"
    sizeof(s)=8 (implementation dependent)
    
    vs[9]={"12":2,"23":2,"345":3,"456":3,"567":3,"67888":5,"7899999999":10,"8":1,"9876543210":10}:39
    sizeof(vs)=24 (implementation dependent)
    
    0 讨论(0)
  • 2020-12-30 07:16

    Better use std::vector<std::string> instead of a raw array. Then you don't have to manually manage the arrays memory and you can use the size() method if you want to know the number of elements.

    If you use a dynamically allocated raw array you are expected to keep track of its size yourself, the size cannot be obtained from the array. Best save it in an extra variable.

    0 讨论(0)
  • 2020-12-30 07:20

    In String vector use size() method

    0 讨论(0)
  • 2020-12-30 07:23

    If what you have is a "real" array, then the sizeof(x)/sizeof(x[0]) trick works. If, however, what you have is really a pointer (e.g. something returned from a function) then that trick doesn't work -- you'll end up dividing the size of a pointer by the sizeof a pointer. They are pointers to different types, but on a typical system all pointers are the same size, so you'll get one. Even when the pointers are different sizes, the result still won't have anything to do with how many strings you have.

    0 讨论(0)
  • 2020-12-30 07:24
    template< class T, size_t N >
    std::size_t Length(const T(&)[N])
    {
        return N;
    };
    
    std::cout << Length(another_array) << std::endl;
    
    0 讨论(0)
  • 2020-12-30 07:27

    You cannot determine the size of an array dynamically in C++. You must pass the size around as a parameter.

    As a side note, using a Standard Library container (e.g., vector) allieviates this.

    In your sizeof example, sizeof(result) is asking for the size of a pointer (to presumably a std::string). This is because the actual array type "decays" to a pointer-to-element type when passed to a function (even if the function is declared to take an array type). The sizeof(result[0]) returns the size of the first element in your array, which coincidentally is also 16 bytes. It appears that pointers are 16 bytes (128-bit) on your platform.

    Remember that sizeof is always evaluated at compile-time in C++, never at run-time.

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