-
December 2nd, 2012, 07:15 PM
#16
Re: Vector iterators
Originally Posted by Paul McKenzie
You need to understand the difference between compile-time and runtime with respect to C++ types. This is a basic fundamental of the C++ and 'C' language.
Code:
#include <string>
#include <iostream>
using namespace std;
int main()
{
std::string s;
cout << sizeof(s) << "\n";
s = "abc123456789123456789xyz";
cout << sizeof(s) << "\n";
}
What results do you get? You see that sizeof(s) is the same, regardless of what you do at runtime to the string? The sizeof(string) doesn't magically change from 0 to 24 just becase it now contains 24 characters.
AH HA MOMENT!
I can see now that when the vector of structs is instantiated, space for the string is not allocated "within" the struct. Just a pointer to the string is the actual member in the struct itself. The string is actually stored elsewhere in memory. So the size of the struct remains constant and the compiler knows exactly how many bytes make up the struct which is always a constant value.
Then all the compiler has to do is:
++ iterator
interator * jumpValue
to get to the next struct.
You're the man Paul. Thanks for helping me understand it! (Hope that's right anyway )
This technique can actually be used in programs I write in the future.
Thanks,
Raptor
Last edited by raptor88; December 2nd, 2012 at 07:17 PM.
-
December 4th, 2012, 11:01 AM
#17
Re: Vector iterators
in all fairness. the real motivation behind why STL works the way it does and why method 1 is preferred, is because it allows you to change the underlying container without changing the rest of code.
If for some reason you decide tomorrow that you would rather have a list instead of a vector, then the iterator approach will continue to compile and run. This flexibility can be a HUGE benefit. And a benefit that is made manifest among others by the various <algorithms> YOu can build your own library of algoritms and templates that are based of containers, and even containers of your own. if you follow the "stl way of doing things", you get the benefit that all those algoritms and other templates will be interchangable (to a degree ofc, there are limits to how much you can make one container behaviourly identical to another).
breaking down the code into 2 or 3 works ONLY under the assumption that the container has a [] operator that takes an index. That's true for vector, but not for other containers.
Using the containers in the way they were intended means you benefit from the flexibility of changing the underlying container if you need to (within reason). It also means you could benefit from improvements in the library and how the compiler handles the STL code (some compilers have specific optimisations for the STL code). Which you may not get from accessing the container via method 2 or 3.
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
|