简体   繁体   中英

Find End of Array Declared as Struct Type C++

I was recently learning to use struct datatype in c++. I know how the basics of struct datatype work and how to manipulate its variables. But I was wondering how would I determine the end of struct datatype array. 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:

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. So how would I determine that I have to loop through only first 30 indexes as the data is only stored in these indexes. As in char array we compare all indexes with '\\0' to determine the end of array. Then what method will we use for Data[] array?

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.).

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 . 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 . 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)

or

use std::array<Data, 30> , which can store exactly 30 objects and you can assume all of them are valid objects.

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() .

Iterating a std::vector or std::array is trivial:

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

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

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

I don't really agree using std::array makes any difference.

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.

How does that work? The ramaining elements, of course, default initialized to be \\0 , they exist , but of no use .

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 .

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.

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...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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