0

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?

1 Answer 1

5

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.