Length of string[] array?

I can't find any method of retrieving the length of an array except for doing this:
1
2
string first[] = {"a","be","see"};
int length = sizeof(first)/sizeof(first[0])

This is a very unconventional way of getting the length of an array.
first->length() would return 1 because it returns the number of letters in the first element of the array (which actually makes no logical sense).
first.size() would return 1 aswell as it's practically the same thing.

Since getting the length of an array is such a fundamental feat, how come I can't find a decent method of doing it?
Is there no buildt in method for this? If there is not, why has it not been implemented in the std?
first->length() would return 1 because it returns the number of letters in the first element of the array (which actually makes no logical sense).

Nice observation, but it does make sense if you understand the rules.

Arrays decay to pointers. This means that when you use an array's name, it becomes the pointer to the first element.

Thus you call the size() member function of the first element in the array, the std::string containing "a".

Since getting the length of an array is such a fundamental feat, how come I can't find a decent method of doing it?

Maybe you'd like to learn a bit about the D language, where things are a bit more... higher level.

As a proficient C++ programmer, you should start using the STL containers and forget about arrays. If you would use for example std::vector the code could look like this:

1
2
3
4
5
#include <vector>
#include <string>

std::vector<std::string> vs {"a", "be", "see"}; // C++11 initializer
std::size_t length = vs.size();

http://www.cplusplus.com/reference/stl/
http://www.stroustrup.com/C++11FAQ.html#init-list
You are confusing what's a standard language "feature" with the Standard Template Library.
std::string is a common class, you could even build it on your own.
While, std::string[] is a C-STYLE ARRAY of common classes.
The common classes will have no knowledge of being in an array.
You can't ask a std::string about its array size.
You must use sizeof, because you can only use sizeof to calculate a C-style array's size.
int length = sizeof(first)/sizeof(first[0]) works because the size of an array is part of the type of the array.

Type of first is std::string[3], so, sizeof(first) == sizeof(std::string) * 3

We could write a function template and use it to deduce the type of the array and return the size. Or use similar templates in the standard library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <string>
#include <iostream>
#include <iterator>
#include <type_traits>

template < typename T, std::size_t N >
constexpr std::size_t size( T(&)[N] ) { return N ; }

int main()
{
    const std::string first[] = { "a", "be", "see" } ;

    std::cout << "array 'first' size: " << size(first) << ' ' ;

    // std::end() overload for arrays uses the same technique
    std::cout << std::end(first) - std::begin(first) << ' ' ;

    // more of the same
    std::cout << std::extent< decltype(first) >::value << '\n' ;


    const int second[][6] = { {0,1}, {2,3,4}, {5}, {6,7,8,9}, {} } ;
    using array_type = decltype(second) ;

    std::cout << "array 'second' rank: " << std::rank<array_type>::value << ", "
               << "nrows: " << std::extent<array_type>::value << ", "
               << "ncols: " << std::extent<array_type,1>::value << '\n' ;
}

http://ideone.com/ezwOYJ
Topic archived. No new replies allowed.