查找数组中某些元素的大小?

时间:2022-10-11 21:26:23

how can I find the size of determined members of an array. For example, I declared my array with this:

如何找到数组中确定的成员的大小。例如,我声明我的数组如下:

string myStrArray[200] = {
    "My first string",
    "My second string",
    "My last string"
}

In this code, there are 197 unused elements (or I understand that so). I want to find this array's certain elements (3 elements) by a code such as sizeof(). How can I do that?

在这段代码中,有197个未使用的元素(或者我理解)。我想通过诸如sizeof()这样的代码找到这个数组的某些元素(3个元素)。我怎么做呢?

4 个解决方案

#1


4  

You cannot. However, you can zero the array first then count the number of non-zero elements but this would require the array to contain string* rather than string.

你不能。但是,您可以先将数组为零,然后计算非零元素的数目,但这将要求数组包含字符串*而不是字符串。

You could use a vector instead, for example:

你可以使用一个向量,例如:

std::vector<std::string> v;
v.reserve(200); // Allocate space for 200
v.push_back("My first string");
v.push_back("My second string");
v.push_back("My last string");
v.size(); // Returns 3

#2


1  

There's no way (at least in C++, what I know) to read out how many elements in array is determined. You have to do it by your own variable (increment it when you "add" elements and decrement when "delete"). You can also use std::vector. vector.size() returns the size of the vector.

不可能(至少在c++中,我知道)读出数组中有多少元素是确定的。您必须使用自己的变量(在“添加”元素时增加它,在“删除”时减少它)。您还可以使用std::vector。size()返回向量的大小。

#3


1  

If you know for sure that all of the non-empty strings are at the beginning, then you can use std::find:

如果您确定所有非空字符串都在开头,那么您可以使用std::find:

int n = std::find(myStrArray, myStrArray + 200, "") - myStrArray;

Actually, you could use std::lower_bound, which is a binary search, and so would be more efficient than std::find. But you'd need a fancy comparison function. One that returns true if the lhs is non-empty and the rhs is empty, false otherwise.

实际上,您可以使用std::lower_bound,它是一个二进制搜索,因此比std::find更有效。但是你需要一个比较函数。如果lhs为非空,rhs为空,则返回true,否则返回false。

If the non-empty elements are sparsely distributed, you will want to use std::count:

如果非空元素分布稀疏,则需要使用std::count:

int n = 200 - std::count(myStrArray, myStrArray + 200, "");

#4


0  

You cannot use sizeof() to find out how many elements are "filled" in the array, you are storing std::string's in the array (I assume atleast) and all elements will be of the same size (because std::string's do not change size after initialization). No objects change size in C++ actually at least not according to sizeof(); sizeof() will always return the same number for a type because it returns the size of the static type.

您不能使用sizeof()来发现数组中“填充”了多少元素,您正在存储std::string在数组中(我认为至少是),所有元素的大小都是相同的(因为std::string在初始化后不会更改大小)。实际上,c++中没有对象更改大小,至少不会根据sizeof()进行更改;sizeof()将总是返回相同的类型,因为它返回静态类型的大小。

If you consider a position in the array to be "filled" when the string does not equal to "", then you can use the following code to count the number of strings:

如果你认为当字符串不等于""时数组中的一个位置被"填充",那么你可以使用下面的代码来计算字符串的数量:

for (int i = 0; i < 200; ++i)
    if (!myStrArray[i].empty()) ++count;

I would recommend using a std::vector<> instead though (that is almost always a better idea):

我建议使用std::vector<>(这几乎总是一个更好的主意):

std::vector<std::string> my_strings = { "a", "b" }; // requires C++11 in C++03 use push_back()
std::cout << "Number of strings: " << my_strings.size() << std::endl;

#1


4  

You cannot. However, you can zero the array first then count the number of non-zero elements but this would require the array to contain string* rather than string.

你不能。但是,您可以先将数组为零,然后计算非零元素的数目,但这将要求数组包含字符串*而不是字符串。

You could use a vector instead, for example:

你可以使用一个向量,例如:

std::vector<std::string> v;
v.reserve(200); // Allocate space for 200
v.push_back("My first string");
v.push_back("My second string");
v.push_back("My last string");
v.size(); // Returns 3

#2


1  

There's no way (at least in C++, what I know) to read out how many elements in array is determined. You have to do it by your own variable (increment it when you "add" elements and decrement when "delete"). You can also use std::vector. vector.size() returns the size of the vector.

不可能(至少在c++中,我知道)读出数组中有多少元素是确定的。您必须使用自己的变量(在“添加”元素时增加它,在“删除”时减少它)。您还可以使用std::vector。size()返回向量的大小。

#3


1  

If you know for sure that all of the non-empty strings are at the beginning, then you can use std::find:

如果您确定所有非空字符串都在开头,那么您可以使用std::find:

int n = std::find(myStrArray, myStrArray + 200, "") - myStrArray;

Actually, you could use std::lower_bound, which is a binary search, and so would be more efficient than std::find. But you'd need a fancy comparison function. One that returns true if the lhs is non-empty and the rhs is empty, false otherwise.

实际上,您可以使用std::lower_bound,它是一个二进制搜索,因此比std::find更有效。但是你需要一个比较函数。如果lhs为非空,rhs为空,则返回true,否则返回false。

If the non-empty elements are sparsely distributed, you will want to use std::count:

如果非空元素分布稀疏,则需要使用std::count:

int n = 200 - std::count(myStrArray, myStrArray + 200, "");

#4


0  

You cannot use sizeof() to find out how many elements are "filled" in the array, you are storing std::string's in the array (I assume atleast) and all elements will be of the same size (because std::string's do not change size after initialization). No objects change size in C++ actually at least not according to sizeof(); sizeof() will always return the same number for a type because it returns the size of the static type.

您不能使用sizeof()来发现数组中“填充”了多少元素,您正在存储std::string在数组中(我认为至少是),所有元素的大小都是相同的(因为std::string在初始化后不会更改大小)。实际上,c++中没有对象更改大小,至少不会根据sizeof()进行更改;sizeof()将总是返回相同的类型,因为它返回静态类型的大小。

If you consider a position in the array to be "filled" when the string does not equal to "", then you can use the following code to count the number of strings:

如果你认为当字符串不等于""时数组中的一个位置被"填充",那么你可以使用下面的代码来计算字符串的数量:

for (int i = 0; i < 200; ++i)
    if (!myStrArray[i].empty()) ++count;

I would recommend using a std::vector<> instead though (that is almost always a better idea):

我建议使用std::vector<>(这几乎总是一个更好的主意):

std::vector<std::string> my_strings = { "a", "b" }; // requires C++11 in C++03 use push_back()
std::cout << "Number of strings: " << my_strings.size() << std::endl;