A vector of structs is a good data structure for a database table. The following table is a products database table:

ProductID ProductName Category Number
1 TV Set Entertainment 10
2 VCD Entertainment 20
3 Clothe Box Household 30
4 Perfume Beauty 40
5 Banana Fruit 50
6 Pear Fruit 60

There are six data rows in the table. The header row (ProductID, ProductName, Category, etc.) is not a data row. Strictly speaking, the header row is not included into (not made part of) the vector of structs in C . This table should be considered as a list of rows of the same row type, beginning from row 1. Strictly speaking, the header row cannot be of the same type as the rest of the rows in the table.

The string in each cell, of the header row, describes the rest of its column data. This article explains how to create a C vector of structs, and its application to creating a database table.

Article Content

– struct

– Construction of Vector of structs

– Feeding Data into the Vector of structs

– Problem of Header Row

– Conclusion

Struct

A struct is similar to a class. Objects are instantiated from a struct. The definition of a struct begins with the reserved word, struct, followed by the generalized name (class name) of the struct, then a pair of braces, within which are struct members. The definition ends with a semicolon after the closing curly bracket. The following code is a struct for each row of the above table; the header row is not considered here:

struct Row {


        unsigned int ProductID;


        string ProductName;


        string Category;


        unsigned int Number;


        float CostPrice;


        float SellingPrice;


    };

Note that ProductName and Category members are strings. This means that the string library has to be included into the program. The generalized name for the struct is, Row. Objects such as row1, row2, row3, etc. can be instantiated from Row. However, specific names for row objects are not necessary for the purpose of this article because “Row” will be the argument of the template parameter for the vector.

Construction of Vector of Structs

The declaration of a vector of characters can be:

where char is the argument of the template parameter for the vector, whose name is, vtr. Similarly, the declaration for the vector of struct Rows would be:

where “Row” is the argument of the template parameter for the vector, whose name can still be, vtr. There are six rows and one header row in the above table. This makes the number of rows, 7. Row 0 is the header row, which may not have any of its own data. The initial number of rows can be indicated in the vector-of-structs declaration.

In order to code a vector in a C program, the vector library has to be included into the program. The head for the program of this article should be:

#include

#include

#include

using namespace std;

In the program, this is followed by the struct Row definition, and then the declaration of the vector of struct Rows before the C main function.

Feeding Data to the Vector of Structs

In order to access an element in the two-dimensional vector-of-structs structure begin with the vector name, e.g., vtr. The is followed by the subscript number in square brackets, which is followed by a dot, and then the name of the column, e.g., ProductID. The following code in the C main function would feed data into rows from row 1 to row 6:

vtr[1].ProductID = 1; vtr[1].ProductName = “TV Set”; vtr[1].Category = “Entertainment”; vtr[1].Number = 10;

vtr[2].ProductID = 2; vtr[2].ProductName = “VCD”; vtr[2].Category = “Entertainment”; vtr[2].Number = 20;

vtr[3].ProductID = 3; vtr[3].ProductName = “Clothe Box”; vtr[3].Category = “Household”; vtr[3].Number = 30;

vtr[4].ProductID = 4; vtr[4].ProductName = “Perfume”; vtr[4].Category = “Beauty”; vtr[4].Number = 40;

vtr[5].ProductID = 5; vtr[5].ProductName = “Banana”; vtr[5].Category = “Fruit”; vtr[5].Number = 50;

vtr[6].ProductID = 6; vtr[6].ProductName = “Pear”; vtr[6].Category = “Fruit”; vtr[6].Number = 60;

After this code segment, the following code segment in the C main function would display all the cell values of the two-dimensional structure:

cout << vtr[1].ProductID <<“, “; cout << vtr[1].ProductName <<“, “; cout << vtr[1].Category <<“, “; cout << vtr[1].Number <<endl;

cout << vtr[2].ProductID <<“, “; cout << vtr[2].ProductName <<“, “; cout << vtr[2].Category <<“, “; cout << vtr[2].Number <<endl;

cout << vtr[3].ProductID <<“, “; cout << vtr[3].ProductName <<“, “; cout << vtr[3].Category <<“, “; cout << vtr[3].Number <<endl;

cout << vtr[4].ProductID <<“, “; cout << vtr[4].ProductName <<“, “; cout << vtr[4].Category <<“, “; cout << vtr[4].Number <<endl;

cout << vtr[5].ProductID <<“, “; cout << vtr[5].ProductName <<“, “; cout << vtr[5].Category <<“, “; cout << vtr[5].Number <<endl;

cout << vtr[6].ProductID <<“, “; cout << vtr[6].ProductName <<“, “; cout << vtr[6].Category <<“, “; cout << vtr[6].Number <<endl;

The output is:

1, TV Set, Entertainment, 10

2, VCD, Entertainment, 20

3, Clothe Box, Household, 30

4, Perfume, Beauty, 40

5, Banana, Fruit, 50

6, Pear, Fruit, 60

A for-loop can be used to print out all the values of the cells. The for-loop would iterate over the rows, beginning from index 1. The for-loop would not iterate any row, because different columns have different names, and each column can be identified by its name.

Problem of Header Row

The header row is at index zero. The header row consists of all strings, but the rest of the rows, do not have only string columns. Well, the header row as one string is:

“ProductID, ProductName, Category, Number”

The second column of the two-dimensional structure takes a string. So, all of this header string can be put in the second cell of row 0. The rest of the cells in row 0 will have their default values. For example, the default value of an int variable is 0.

Code can be written to separate this header string into its column header strings for display of the whole table including the header row but that is not addressed in this article.

Making the second cell of row 0 have the whole header string is simple as follows:

vtr[0].ProductName = “ProductID, ProductName, Category, Number”;

Note the index of 0 for row 0. Reading all of it out as one string is also simple as follows:

string str = vtr[0].ProductName;

cout << str << endl;

The output should be:

ProductID, ProductName, Category, Number

Separating the whole string into column header strings may be an issue – that is discussion for some other time.

Conclusion

To create a vector of structs, define the struct, with a generalized (class) name. Make the template argument of the vector of interest, the generalized name of the struct. Access each cell of the two dimensional structure with the syntax, vtr[i].columnName.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/Chrysanthus-150×150.jpeg6265c671a2354.jpg" height="112" src="data:image/svg xml,” width=”112″>

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.