在 C++ 中查找字符串数组的大小

问题描述 投票:0回答:12

所以我有一个字符串数组。这是一个例子:

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

有什么办法可以找到像上面那样的数组中的元素数量。我不能用这个方法:

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

这是因为元素的大小不同。还有别的办法吗?

c++ arrays c sizeof
12个回答
45
投票

鉴于您的字符串数组,您当然可以使用

sizeof(array)/sizeof(array[0])
来获取它的大小,并且以下程序可以正常工作:

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);
}

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

然而,在某些情况下上述方法不起作用。考虑以下示例:

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

上面的代码注定要失败。乍一看可能有点奇怪,但其实原因很简单——这就是所谓的数组衰减。这意味着每次将数组传递给函数时,它的类型都会自动衰减为指针的类型。所以上面的函数实际上等同于:

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

如果在第一个示例中

sizeof
运算符返回数组的总大小,那么在第二个示例中它返回指向该数组的指针的大小,这是完全不同的事情。

人们通常有两种处理方式。第一个是为数组添加一个特殊的“最后一个”元素,以便应用程序可以遍历数组直到它看到最后一个元素并计算数组的长度。字符串文字就是一个完美的例子——每个字符串文字都以‘’结尾,你总是可以计算它的长度。这是一个例子:

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

缺点显然是需要遍历数组来确定它的长度。第二种方式总是携带数组长度,例如:

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++中,可以使用模板来扣除数组的长度,例如:

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/container

希望有帮助。祝你好运!


7
投票

您可以使用模板函数来实现:

#include<cstdlib>

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

就像 Luchian Grigore 说的,你应该使用 STL 容器。 std::array 如果你想等同于静态 C 数组。


5
投票

你仍然可以使用

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

这是因为

sizeof(array)
会返回每个字符串对应的指针大小之和。
sizeof(array[0])
将返回对应于第一个字符串的指针的大小。因此,
sizeof(array)/sizeof(array[0])
返回字符串的数量。


5
投票

我们可以简单地使用

size()
函数来找到数组的元素数。

示例代码:

string exampleArray[] = { "Example", "Example2", "Example3" };
    int size_of_array = size(exampleArray);

cout << "Number of elements in array = " << size_of_array << endl;

输出:

>>> Number of elements in array = 3

希望对你有帮助


4
投票

stdlib库中有标准函数:

#include <stdlib.h>
static const char * const strings[] = {"str1", "str2", "str3"};
const int stringCount = _countof(strings);

1
投票

这是你要找的吗?

string array[] = {"Example", "Example2", "Example3"};
int num_chars = 0;
int num_strings = sizeof(array)/sizeof(array[0]);
for (int i = 0; i < num_strings; i++) {
    num_chars += array[i].size();
}

1
投票

强制性建议:使用

std::vector<std::string>
.

这样的功能可以帮助:

template<typename T, int sz>
int getSize(T (&) [sz])
{
    return sz;
}

您的方法也有效,因为元素的大小不变 -

std::string
具有恒定大小,无论它包含的实际字符串如何。


1
投票

这是一个不同的例子:

string array[] = {"Example", "Example2", "Example3"};
int numberofelements = 0; for(auto c: array) { numberofelements++; };
// now numberofelements will contain 3

0
投票
string s[] = {"apple","banana","cherry","berry","kiwi"};
int size = *(&s+1)-s;  
// OR
int size = sizeof(s)/sizeof(s[0]);

有关第一个的更多信息: https://aticleworld.com/how-to-find-sizeof-array-in-cc-without-using-sizeof/


0
投票

实际上没有人计算重复项所以:

int count_duplicated(string cities[], size_t length) {
    string **cities_copy = new string *[length];
    for (size_t j = 0; j < length; j++)
        cities_copy[j] = &cities[j];

    string bom = "#";
    int counter = 0;
    for (size_t j = 0; j < length; j++) {
        string city = cities[j];
        bool is_duplicated = false;


        for (size_t k = j+1; k < length; k++) {
            if (city == *cities_copy[k]) {
                is_duplicated = true;
                cities_copy[k] = &bom;
            }
        }
        if (is_duplicated)
            counter++;
    }
    delete[] cities_copy;
    return counter;
}

0
投票

注意:sizeof(array) 将不再返回 Windows 中数组的完整大小,而仅返回指针的大小。 Windows 中的标准库已经悄悄地改变了这一点,以允许使用它们的附加库,例如 std:array 和 std::iterator。您将需要使用这些库中的函数或创建您自己的方法来计算元素。这是针对那些由于库冲突或其他原因而无法使用 std::array 或 std::iterator 的示例。

size_t SizeOfArray(std::string* inComing)
{
  size_t outGoing = 0;
  bool end = false;
  // Validate Array
  if (inComing != NULL)
  {
    // Appended arrays can be valid or not. Sometimes earlier cleanup
    // will link to an empty array location, so check for empty and invalid. 
    while ((!end) && (inComing[outGoing].size() != 0))
    {
      __try
      {
        // if an appended memory location has value, but is not an array
        // you look for the NULL character. For wstrings change '\0' to L'\0'.
        // If the memory location is not valid, it will throw the exception.
        if (inComing[outGoing].c_str()[inComing[outGoing].length()] != '\0')
        {
          // End of array -  if nice empty value
          end = true;
        }
        else
        {
          outGoing++; // Count each valid string in the array
        }
      }
      // This is the blank exception catch to an extra element that is not
      // valid.
      __except (EXCEPTION_EXECUTE_HANDLER)
      {
      // End of array -  if unknown value
      end = true;
      }
    }
  }
  return outGoing; // Return the count
}

-1
投票

我测试了这段代码,它工作正常:

string as[] = {"kayak","deified","rotator","repaper","deed","peep","wow","noon"};

int asize = as->length();
© www.soinside.com 2019 - 2024. All rights reserved.