Re: Vector iterators. How do they work with strings in a struct?
A new question regarding vectors and iterators.
I declared a struct that contains integers and one string. Then I instantiated a vector to hold the structs and set the total number of structs. My actual struct is pretty large so here's a sample to keep things simple:
Code:
struct MyStruct
{
int a
int b
string c
}
vector<MyStruct> myVect[8];
for(vector<MyStruct>::size_type i = 0; i < myVect.size(); ++i)
{
// do something.
}
Although I did not show it in the code above to keep things simple, I initialized the members of the struct and the strings are variable lengths. Also I know it's possible to use "push_back" to add elements to the vector but I have a reason why I need to establish the total size of the vector at time of instantiation. Also, my actual code compiles and works so that's not an issue.
MY QUESTION:
When I instantiated myVect with 8 elements, how did the compiler know how much memory to reserve and how to lay things out in memory AT THE TIME OF INSTANTIATION, when I initialized the structs in the vector with variable length strings AFTER the instantiation was done?
Then how does incrementing the iterator "i" by only +1 each time work when each struct is effectively variable length in size? What are the actual mechanics involved?
Thanks,
Raptor