|
-
December 2nd, 2012, 02:10 AM
#11
Re: Vector iterators
I note that this creates an array of 8 vector<MyStruct> objects:
Code:
vector<MyStruct> myVect[8];
If you really wanted a vector of 8 MyStruct objects, then it should have been:
Code:
vector<MyStruct> myVect(8);
 Originally Posted by raptor88
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?
The memory used is for the MyStruct objects with the empty string members. If you change the string members later, more memory might be allocated at that later point.
 Originally Posted by raptor88
Then how does incrementing the iterator "i" by only +1 each time work when each struct is effectively variable length in size?
Each MyStruct object has the same size with respect to sizeof. Anyway, the concept of an (input) iterator abstracts away this consideration, i.e., you just need to know that incrementing the iterator causes it to point to the next element.
 Originally Posted by raptor88
What are the actual mechanics involved?
That depends on the implementation. Of course, since we are dealing with a vector here, you can imagine having a pointer to an element in a dynamic array. Incrementing the pointer would thus cause it to point to the next element.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|