Nested Structs or Vector of Structs?

Apr 29, 2012 at 7:13am
I have a structure with a vector of strings inside.

I basically want a way to assign the different strings in the vectors different properties.

I'm currently have an owners (a struct) that each own a vector of cats (vector<string> cat)

For example, currently, I have working:
Scarlett.cat.at(0) == "Boots"
Now I want a way to assign Boots a weight and color.
Would I have to make a cat structure with the cats information?
Or would I make a vector with datatype cat, and then have all of the information?
Would the cat name be in the new struct, or would it be to differentiate between the vectors?
I'm just really confused at how I would do this. My source code is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct cat
{
	string name;
	string color;
	int weight;
};

struct Owner
{
	vector<string> cat;
};

void main()
{
	Owner Sarah, Scarlett;
	
	Sarah.cat.push_back("Carlisle");
	Sarah.cat.push_back("Oliver");
	Scarlett.cat.push_back("Boots");
	Scarlett.cat.push_back("Midnight");

	Sarah.cat.at(0).color("white"); // this is an error
	Sarah.cat.at(1).color("gray"); // this is an error
	Scarlett.cat.at(0).color("gray and white"); // this is an error
	Scarlett.cat.at(1).color("black"); // this is an error

	system("pause");
}

I basically want to do what I was trying to do above, where I tried assigning the cats a color (but I got an error).

I want to be able to add/change their colors and weights at different times in the program.

I'd appreciate it if you could show me code and an explanation of how you got there so that I can understand. Thank you!

====
edit: someone on another site showed me that I could do this:

1
2
3
4
5
6
7
8
9
10
struct cat
{
	cat();
	cat(string name, string color, int weight);
};

struct Owner
{
	vector<cat> info;
};


And then use:
Sarah.info.push_back(*(new cat("Tim", "red", 2)));

but I need to be able to give it the attributes "red" and 2 (color and weight) at different times, later in the program, other than initialization. How would I do this? (in other words, how would I go about storing the individual elements of the structure vector?)
Last edited on Apr 29, 2012 at 8:02am
Apr 29, 2012 at 8:35am
Sarah.cat.at(0).color("white");
This is an error because color is not a function. Use operator= to set the new value of the string.
Sarah.cat.at(0).color = "white";
Apr 29, 2012 at 10:57am
This is very bad:
 
Sarah.info.push_back(*(new cat("Tim", "red", 2)));

It leaks memory because the new cat you create is copied by value into your vector, and the address of the original is lost.

What you can do is something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct cat
{
	string name;
	string color;
	int weight;
};

struct Owner
{
	vector<cat> cats;
};

//void main() // this is wrong, main must always return int
int main()
{
	Owner Sarah, Scarlett;

	// Make the vector 2 cats big

	Sarah.cats.resize(2);
	Scarlett.cats.resize(2);

	// Set the names and colors of each cat

	Sarah.cats[0].name = "Carlisle";
	Sarah.cats[0].color = "white";
	Sarah.cats[1].name = "Oliver";
	Sarah.cats[1].color = "gray";

	Scarlett.cats[0].name = "Boots";
	Scarlett.cats[0].color = "gray and white";
	Scarlett.cats[1].name = "Midnight";
	Scarlett.cats[1].color = "black";

	cat c;

	// Extend the vector by adding another cat

	c.name = "Apadaple";
	c.color = "Tortoise shell";

	Sarah.cats.push_back(c); // makes a copy of c

	// And another one

	c.name = "Dorian";
	c.color = "Gray";

	Sarah.cats.push_back(c); // makes a copy of c

	system("pause");
}
Apr 29, 2012 at 1:37pm
Thank you! This is exactly what I was looking how to do!
Topic archived. No new replies allowed.