在C ++中查找String数组的大小 [英] Finding the size of an String array in C++

查看:96
本文介绍了在C ++中查找String数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个字符串数组.这是一个示例:

So I have an array of strings. Here's an example:

string array[] = {"Example", "Example2", "Example3"};

有什么办法可以找到上述数组中数组中元素的数量.我不能使用这种方法:

Is there any way I can find the number of elements in an array like the one above. I can't use this method:

int numberofelements = sizeof(array)/sizeof(array[0]);

这是因为元素的大小各不相同.还有另一种方法吗?

This is because the size of the elements vary. Is there another way?

推荐答案

给出您的字符串数组,您当然可以使用sizeof(array)/sizeof(array[0])来获取其大小,并且以下程序可以正常运行:

Given your array of strings, you can most certainly use sizeof(array)/sizeof(array[0]) to get its size and the following program works just fine:

int main()
{
    std::string array[] = { "S1", "S2", "S3" };
    std::cout << "A number of elements in array is: "
              << sizeof(array)/sizeof(array[0]) << '\n';
    foo(array);
}

不清楚说元素的大小是什么意思是什么.任何数组元素的大小在编译时都是已知的,没有例外.

It is not clear what do you mean by saying that size of elements vary. Size of the elements of any array is always known at compiler-time, no exceptions.

但是,在某些情况下,上述操作将无效.考虑以下示例:

There are, however, situations where the above will not work. Consider the following example:

void foo(std::string array[])
{
    std::cout << "A number of elements in array is: "
              << sizeof(array)/sizeof(array[0]) << '\n';
}

上面的代码注定会失败.乍一看可能有点奇怪,但是这样做的原因实际上很简单-这称为数组衰减.这意味着每次将数组传递给函数时,其类型都会自动衰减为指针的类型.因此,上述功能实际上等效于此:

The above code is doomed to fail. It might look a bit weird at first, but the reason for this is actually very simple — this is called array decaying. It means that every time you pass an array to a function, its type is automatically decayed to that of a pointer. So the above function is in fact an equivalent of this:

void foo(std::string *array)
{
}

如果在第一个示例中,sizeof运算符返回数组的总大小,在第二个示例中,它返回指向该数组的指针的大小,则完全不同.

And if in the first example the sizeof operator returns the total size of an array, in the second example it returns the size of a pointer to that array, which is a totally different thing.

人们通常有两种解决方法.首先是在数组中添加一个特殊的"last"元素,以便应用程序可以遍历该数组直到看到最后一个元素并计算数组的长度.字符串文字是一个很好的例子-每个字符串文字都以"\ 0"结尾,您可以随时计算其长度.这是一个示例:

There are usually two ways people go about it. The first is to add a special "last" element of the array so that application can traverse the array until it sees the last element and calculate the array’s length. String literals are the perfect example of this — every string literal ends with ‘\0’ and you can always calculate its length. Here is an example:

static void foo(const std::string *array)
{
    size_t i = 0;
    while (!array[i].empty())
        ++i;
    std::cout << "Array length is: " << i << std::endl;
}

显然,需要遍历数组以确定其长度.始终携带数组长度的第二种方法,例如:

The downside is obviously a need to traverse the array to determine its length. The second way it to always carry array length around, for example:

static void foo(const std::string *array, size_t length)
{
    // ...
}

void bar()
{
    std::string array[] = { "S1", "S2", "S3" };
    foo(array, sizeof(array)/sizeof(array[0]));
}

在C ++中,您可以使用模板来推断数组的长度,例如:

In C++, you can use a template to deduct array’s length, for example:

template <size_t array_length>
static void foo(const std::string (&array)[array_length])
{
    std::cout << "A number of elements in template array is: "
              << array_length << '\n';
}

以上所有内容均适用于语言内置的简单数组.另一方面,C ++提供了一组丰富的高级容器,这些容器为您提供了很大的灵活性.因此,您可能要考虑使用作为C ++标准库的一部分可用的容器之一.有关标准容器的列表,请参见— http://en.cppreference.com/w/cpp/容器

All of the above applies to simple arrays that are built-in into the language. C++, on the other hand, provides a rich set of higher-level containers that give you a lot of flexibility. So you might want to consider using one of the containers that are available to you as part of C++ Standard Library. For a list of standard containers, see — http://en.cppreference.com/w/cpp/container

希望它会有所帮助.祝你好运!

Hope it helps. Good Luck!

这篇关于在C ++中查找String数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆