1

I have a program where I use records of the form:

// declaring a struct for each record
struct record
{
    int number;             // number of record
    vector<int> content;    // content of record    
};

Within main I then declare each record:

record batch_1;         // stores integers from 1 - 64
record batch_2;         // stores integers from 65 - 128

Where each batch stores 64 integers from a list of numbers (in this instance from a list of 128 total numbers). I would like to make this program open ended, such that the program is capable of handling any list size (with the constraint of it being a multiple of 64). Therefore, if the list size was 256 I would need four records (batch_1 - batch_4). I am not sure how I can create N-many records, but I am looking for something like this (which is clearly not the solution):

//creating the batch records
for (int i = 1; i <= (list_size / 64); i++)
{
    record batch_[i];   // each batch stores 64 integers
}

How can this be done, and will the scope of something declared within the for loop extend beyond the loop itself? I imagine an array would satisfy the scope requirement, but I am not sure how to implement it.

2
  • Why not use vector<record> batch(list_size / 64) and then init it as needed in a loop? You're already using vectors. Commented Apr 1, 2017 at 17:57
  • "I am not sure how I can create N-many records". You have used vector in the definition of your record. How would you define its function? Commented Apr 1, 2017 at 19:13

2 Answers 2

2

Like many suggested in the comments why not use a resizable vector provided by the C++ Standard Library: std::vector?

So, instead of having this:

record batch_1;         // stores integers from 1 - 64
record batch_2;         // stores integers from 65 - 128
.
.
record batch_n          // Stores integers x - y

Replace with:

std::vector<record> batches;

//And to create the the batch records
for (int i = 1; i <= (list_size / 64); i++) {
    record r;
    r.number = i;
    r.content = ....;
    batches.push_back(r); 
    // You could also declare a constructor for your record struct to facilitate instantiating it.
}
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you try this

    // code 
    vector<record> v(list_size / 64);
    // additinal code goes here

Now,you can access your data as follow

    (v[i].content).at(j);

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.