I have a struct as follows:
struct Database
{
int accountNum;
double balance;
int months;
};
I want to make a vector of this struct, because I want the size to vary. How do I do that?
Thanks in advance
I have a struct as follows:
struct Database
{
int accountNum;
double balance;
int months;
};
I want to make a vector of this struct, because I want the size to vary. How do I do that?
Thanks in advance
I think you need to be a little more clear about your intentions. What exactly do you mean by "I want the size to vary"?
Do you want to use a vector so that the number of Database objects can vary?
Or do you want the actual size of a Database object to vary?
The size of the Database object of course. Otherwise I'd just use an array.
I could do Database info[20] for 20 accounts, but the user may want to look at 25 accounts, or 100 accounts.
So I figure a vector is the way to go.
Sorry about that :|
>>The size of the Database object of course. Otherwise I'd just use an array.
:icon_frown:I think you need to watch your tongue until you actually know what you are talking about.
What you are talking about in your example is the number/count of Database objects, NOT the size of a single Database object. Per that example, the size of a Database object will remain 16 bytes*[1]. What changes is the number of 16-byte database objects that are in existence at any given point in time. For your situation, a vector is a good approach.
Q. How would you declare a vector of int?
A. By telling the vector that it's full of int objects:
vector<int> myIntVec;
Based on that answer, where do you think you should put "struct Database"?
*[1]Depending on byte alignment, it could also be 24-bytes
...What? That isn't what I mean at all.
I could do Database info[20] for 20 accounts, but the user may want to look at 25 accounts, or 100 accounts.
As I said, I can easily make an array of struct. I could declare Database info[20], which means I have an array, info, of type struct Database and the size of the array is 20.
However, with a vector, the size can change dynamically, which I want. Instead of 20, I want to have 100 or 25 or even 1, not JUST 20.
My question is how can I create a vector of instead of an array in this case.
>>What? That isn't what I mean at all.
Unless you haven't provided an accurate example of your intentions, it's exactly what you mean. Is this, or is this not exactly what you said:
I could do Database info[20] for 20 accounts, but the user may want to look at 25 accounts, or 100 accounts.
>>I could do Database info[20] for 20 accounts
You are correct that this creates an array that contains 20 elements. However, you are incorrect about what you are actually creating. Those 20 elements are all individual Database instances/objects. As such, those array elements are NOT inside Database; Database is inside the array elements.
Do you even understand what a struct/class is and what objects are and the relationship between them? I don't think you do. A struct/class is like a blueprint for a building. An object is like an actual building based on that blueprint.
Read this, then come back and see if you understand what I told you earlier.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.