CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #16
    Join Date
    May 2012
    Posts
    57

    Re: Vector iterators

    Quote Originally Posted by Paul McKenzie View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured