struct vs array

what is the use of struct if i can just use array rather than creating object of a struct again and again
if you can
That is a big if. Not quite sure what you mean.
Please show an example; both struct and array versions.
closed account (SECMoG1T)
You can use an array to hold many objects of struct type instead of creating a single object everytime
But you can never substitute a struct for an array and vise versa, an array can hold multiple objects of struct type, however your question is not clear enough to deduce your idea clearly, I have just assumed that what you ment.
Last edited on
@keskiverto
example:
for struct
1
2
3
4
5
6
7
struct shop {
string weapon_name;
int price;
}
shop firstWeapon = { "first weapon" , 100 };
shop secondWeapon= { "second weapon" , 200 };
shop ThirdWeapon = { "third weapon" , 300 };

for array
1
2
3
4
5
6
7
8
string weapon_name[2];
int price[2];
weapon_name[0] = "first weapon"
weapon_name[1] = "second weapon"
weapon_name[2] = "third weapon"
price [0] = 100;
price [1] = 200;
price [2] = 300;

@andy1992
i mean, you know, they both collect data instead declaring multiple data types

read my above comment , just like it , whats the use of that struct if i can do that array , thats what i ment
Last edited on
Lorence30:
If you want to pass a structure to a function, it's easier to pass the struct. Otherwise you'd have to pass each individual field, or the index.

Or what if you want to output the struct to a stream? It's easier to create operator <<() on the struct and then just do cout << myInstance;

What if you want member functions? Or private members? You can't do those with individual arrays.

There is potentially a performance penalty too: if you store the bits of the structure in separate arrays and there are lots of them, then each time you access one, you're hitting a different page in memory, possibly triggering a page fault.

Hope this helps.
@dhayden i dont really understand :(
Imagine following data:
Surname
Name
Middle name
DOB
Phone №
Address

You have 100 records of it.

Now you need to sort in by Surname and same Surnames should be sorted by Name.
With array of structs I can do this with two lines. Thing how you would do this with parallel arrays.
I can remove all persons livin on Oak st. with one line. Try to do this with parallel arrays

Structs support custom constructors, methods, polymorfism, encapsulation and much more. Arrays are not.
There is potentially a performance penalty too

Or benefit. If the separate values are in different cache lines (but no page fault), then accesses might actually improve. Either way, this is hardware-specific deep magic.


@Lorence30:
1
2
3
4
5
6
7
8
9
10
struct Shop {
  std::string weapon_name;
  int price;
};

std::vector<Shop> shops {
  { "first weapon" , 100 },
  { "second weapon" , 200 },
  { "third weapon" , 300 }
};
I think you're missing that you can also have an array of structs.
Structs or classes are ideal for keeping related information.
Keeping data in individual arrays as in your second snippet loses the relationship between the information (name & price).

Modifying your first snippet:
1
2
3
4
5
6
7
8
9
10
struct shop 
{ string weapon_name;
  int price;
};
// Create an array of weapons
shop weapons[3] = 
{ "first weapon" , 100, 
   "second weapon" , 200, 
   "third weapon" , 300
};


As dhayden pointed out, I can pass a specific weapon to a function.
1
2
 
  display (weapons[2]);


Here I would take exception to your naming the struct "shop". An instance of your struct is not representing a shop, but rather a particular item of inventory. So I would call it an Item. A shop or store typically has a collection of Items to sell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct Item 
{   
private:
     string name;
     int price; 
     int on_hand;  //  How many of this item are available

public:
    //  Construct an Item
    Item (string nm, int pr, int oh);

    //  Actions to take on item 
    void Sell (int qty);
    void restock (int qty);

    // Print out an item
    friend ostream & operator << (ostream & os, const Item & item);
};




Topic archived. No new replies allowed.