Thread: Possible causes for strange vector display?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    Possible causes for strange vector display?

    In my code I have a vector that should be a size of 1, containing the integer value 2.

    When this portion of the code runs:
    Code:
    std::cout << "Root size: " << root->key.size() << "\n";
    
    typename std::vector<T>::iterator it = root->key.begin();
    for(; it != root->key.end(); ++it){
        std::cout << *it << " ";
    }
    and the output is as I expect:
    Code:
    Root size: 1
    2
    But when I have this in my code:
    Code:
    Node* curr = root;
    std::cout << curr->key[0] << "\n";
    std::cout << curr->key[1] << "\n";
    std::cout << curr->key[2] << "\n";
    This is the output:
    Code:
    2
    2
    0
    So what's with the extra "2" in the output?

    Is it because operator[] returns a reference and perhaps it's referencing some erroneous data? I'm confused because the size() is correct and the iterator display works correctly...

    I'm preplexed...

    BTW: replacing curr->key[x] with root->key[x] makes no difference, curr doesn't point to anything else in the function either.
    Last edited by dudeomanodude; 04-08-2008 at 11:10 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed