soniccool
soniccool

Reputation: 6058

How to push value into a vector within a struct

I have the struct below

How can i push into a vector within that struct?

struct DNA
{
    vector <string>header;
    string DNAstrand;
    double gc;
    int valid;
};
struct World
{
    //  int     numCountries;
    DNA dnas[MAX_DNA_SIZE];
} myWorld;

I wish to push a string lets say the string variable is line into the vector called header in my dna struct.

How would i go about doing so? I know that if i wish to add an element into the DNAstand i would just use myWorld.dnas[counter].DNAstrand = line But how does this work when i have a vector in there?

Upvotes: 0

Views: 187

Answers (1)

Paul92
Paul92

Reputation: 9062

It works the same way as with a regular vector. Let's say you have:

vector<string> a;

Then you would do:

a.push_back(line);

In your case, it's the same, just the vector's name is myWorld.dnas[counter].header, so you would do:

myWorld.dnas[counter].header.push_back(line);

Upvotes: 5

Related Questions