Thread: std::multimap<A,B>::iterator

  1. #1
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    std::multimap<A,B>::iterator

    OK, I'm tired and it's late, but never mind:

    I have:
    Code:
    using namespace std;
    
    multimap<string, Function> Functions;
    ...
    multimap<string, Function>::iterator ifunc;
    for (ifunc = Functions.begin(); ifunc < Functions.end(); ++ifunc)
    {
    ...
    }
    This generates one error of unbelivable length (you know STL errors...)

    When I change the < to != it works, but shouldn't < work as well?
    Is multimap special?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  2. #2
    What value does Functions.end() return? a number or Bool or other??
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    it returns multimap<string, Function>::iterator as defined by the standard.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm, perhaps you could overload that one yourself? How would you define .end() -1 though?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    >When I change the < to != it works, but shouldn't < work as well?

    No. "<, >" will only work when the data is stored contiguously in memory (like in an array), but since a map might be implemented with a graph of some sort, each node can be in different locations in memory, not one-after-the-other, hence it is useless to compare the address with the less-than and greater-than operators
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    OK, that must be the explanation.

    But, since ++ can be defined, why not < ?
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    ++ and -- operators for the iterator class used with map/multimaps are probably implemented by using an embedded pointer, like with nodes used in a list where, conceivably at least---I've never tried it---you could go like this:

    Code:
    struct node
    {
       //data
       node * next;
       void operator++;
    };
    
    void node::operator++
    {
       this = this->next;
    }
    I know, the code above is probably full of bugs, but the process should work, once the details are worked out. The added wrinkle here would be that rather than a list, there is probably a tree underlying map/multimap somewhere.

    Thus, ++ and -- are only concerned with the next or previous node relative to current node, not some node that may be x number of nodes away from current node, which is what < or > implies.
    Last edited by elad; 10-24-2002 at 12:18 PM.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Of course.

    I guess I'm still tired.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed