简体   繁体   English

查找声明为 C++ 结构类型的数组结尾

[英]Find End of Array Declared as Struct Type C++

I was recently learning to use struct datatype in c++.我最近正在学习在 C++ 中使用struct数据类型。 I know how the basics of struct datatype work and how to manipulate its variables.我知道struct数据类型的基础知识如何工作以及如何操作其变量。 But I was wondering how would I determine the end of struct datatype array.但我想知道如何确定struct数据类型数组的结尾。 For example consider the code below:例如考虑下面的代码:

struct PersonDetails
{
    string name, address;
    int age, number;
}

Now in c++ program I create an array of struct type as follows:现在在 C++ 程序中,我创建了一个结构类型的数组,如下所示:

PersonDetails Data[500];

Now consider that I have 30 records in data array and I have to display these records by looping through data array's index.现在考虑我在数据数组中有 30 条记录,我必须通过循环访问数据数组的索引来显示这些记录。 So how would I determine that I have to loop through only first 30 indexes as the data is only stored in these indexes.那么我如何确定我只需要遍历前 30 个索引,因为数据仅存储在这些索引中。 As in char array we compare all indexes with '\\0' to determine the end of array.就像在char数组中一样,我们将所有索引与'\\0'进行比较以确定数组的结尾。 Then what method will we use for Data[] array?那么我们将使用什么方法来处理Data[]数组呢?

An edit that I have no idea about Vectors and the project i am working on requires me to use basics of c++(functions, control structures, loops, etc.).我对 Vectors 和我正在从事的项目一无所知的编辑要求我使用 C++ 的基础知识(函数、控制结构、循环等)。

It's not feasible.这是不可行的。

For char[] , back in times of C standardization, developers agreed to use \\0 (integer value 0 ) as a special character marking end-of-string .对于char[] ,回到 C 标准化时代,开发人员同意使用\\0 (整数值0 )作为标记字符串结束的特殊字符。 Everything works as long as everyone is following this convention (ie both standard library functions and developers using those functions).只要每个人都遵循此约定(即标准库函数和使用这些函数的开发人员),一切都可以正常工作。

If you wanted to have such a convention for your type, you could just write down " Data object with both strings empty and both ints equal to 0 is array terminator ", but you would have to follow this convention .如果你想为你的类型制定这样的约定,你可以写下“两个字符串都为空且两个整数都等于0 Data对象是数组终止符”,但你必须遵循这个约定 You'd have to write functions that would stop processing array upon finding such an object.您必须编写在找到此类对象时停止处理数组的函数。 You'd have to make sure that in every array there is at least one such object.您必须确保在每个数组中至少有一个这样的对象。

Instead反而

You should use std::vector<Data> which can automatically accomodate for any number of Data objects and will now precisely how many of them are currently stored (using size() method)您应该使用std::vector<Data> ,它可以自动容纳任意数量的Data对象,并且现在可以精确地存储当前存储的数量(使用size()方法)

or或者

use std::array<Data, 30> , which can store exactly 30 objects and you can assume all of them are valid objects.使用std::array<Data, 30> ,它可以存储正好30 个对象,您可以假设它们都是有效对象。

IMHO the correct way to solve this is to not use a C-style array, but instead use a std::array or std::vector that knows it's .size() .恕我直言,解决这个问题的正确方法是使用 C 风格的数组,而是使用知道它是.size()std::arraystd::vector

Iterating a std::vector or std::array is trivial:迭代std::vectorstd::array是微不足道的:

for (const auto& element : Data_array) {
    // Do something with the array element
}

See also:也可以看看:

https://en.cppreference.com/w/cpp/container/array https://en.cppreference.com/w/cpp/container/array

https://en.cppreference.com/w/cpp/container/vector https://en.cppreference.com/w/cpp/container/vector

https://en.cppreference.com/w/cpp/language/for https://en.cppreference.com/w/cpp/language/for

https://en.cppreference.com/w/cpp/language/range-for https://en.cppreference.com/w/cpp/language/range-for

I don't really agree using std::array makes any difference.我真的不同意使用std::array有什么不同。

The problem you currently have doesn't occur in whether we have such an element in the container , but whether the element we are inspecting useful.您目前遇到的问题不在于容器是否有这样的元素,而是我们正在检查的元素是否有用。

Consider the example you gave, for an array of char s, we simply check whether one of the elements is \\0 to decide whether or not we should halt the iteration.考虑您给出的示例,对于char数组,我们只需检查其中一个元素是否为\\0来决定是否应该停止迭代。

How does that work?这是如何运作的? The ramaining elements, of course, default initialized to be \\0 , they exist , but of no use .拉曼元素,当然,默认初始化为\\0 ,它们存在,但没有用

Similarly, you can check, in this example, whether同样,在本例中,您可以检查是否

name.empty()

Or, in order to avoid any possible exception, as mentioned in the comment section, do this:或者,为了避免任何可能的异常,如评论部分所述,请执行以下操作:

add user-defined constructor to the class ( or struct, they are same actually.) which initialize age to -1 and then check if age == -1 .将用户定义的构造函数添加到类(或结构,它们实际上是相同的。)将 age 初始化为-1 ,然后检查是否age == -1

because it's impossible for a people not having any name, that means, you have not assign to any of the remaining elements.因为对于一个没有任何名字的人来说是不可能的,这意味着你没有分配给任何剩余的元素。 Thus, stop iteration.因此,停止迭代。

As a supplement, using std::vector makes sense, but if that isn't a option for you for the time being, you don't need to consider it.作为补充,使用std::vector是有道理的,但如果暂时不适合您,则无需考虑。

The simplest solution is to just have a separate variable specifying how many array elements are filled in.最简单的解决方案是使用一个单独的变量来指定填充了多少数组元素。

PersonDetails Data[500];
int numPersons = 0;

Data[0].name = ... ;
Data[0].address = ...;
Data[0].age = ...;
Data[0].number = ...;
numPersons = 1;

Data[1].name = ... ;
Data[1].address = ...;
Data[1].age = ...;
Data[1].number = ...;
numPersons = 2;

...

Then you use that variable when looping through the array.然后在遍历数组时使用该变量。

for (int i = 0; i < numPersons; ++i)
{
    // use Data[i] as needed...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM