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

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

问题描述

所以我有一个字符串数组.举个例子:

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

std::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.

人们通常有两种方法.第一种是在数组中添加一个特殊的最后"元素,以便应用程序可以遍历数组,直到看到最后一个元素并计算数组的长度.字符串字面量就是一个完美的例子——每个字符串字面量都以‘\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++ 中查找字符串数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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