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