Problem with distance and STL map

Discussion in 'C++' started by tron.thomas@verizon.net, Sep 13, 2005.

  1. Guest

    I have tried the following code on three different compilers and all
    three produce programs that hang and fail to complete successfully.

    #include <map>
    #include <string>
    #include <iostream>
    #include <iterator>

    int main()
    {
    typedef std::map<int, std::string> Values;
    typedef Values::iterator Iterator;
    Values values;

    values[1] = "one";
    values[2] = "two";
    values[3] = "three";
    values[4] = "four";
    values[5] = "five";

    Iterator first = values.find(4);
    Iterator second = values.find(2);

    std::cout << "The distance is " << std::distance(first, second) <<
    ".\n";

    return 0;
    }

    I did not find any documentation that would indicate this should not
    work.
    How valid is the use of distance with an SLT map?
     
    , Sep 13, 2005
    #1
    1. Advertising

  2. Pete Becker Guest

    wrote:
    >
    > I did not find any documentation that would indicate this should not
    > work.
    > How valid is the use of distance with an SLT map?
    >


    For anything other than a random access iterator, distance increments
    its first argument until it's equal to its second argument. Since the
    first argument in the code follows the second argument, distance can't work.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Sep 13, 2005
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    >I have tried the following code on three different compilers and all
    > three produce programs that hang and fail to complete successfully.
    >
    > #include <map>
    > #include <string>
    > #include <iostream>
    > #include <iterator>
    >
    > int main()
    > {
    > typedef std::map<int, std::string> Values;
    > typedef Values::iterator Iterator;
    > Values values;
    >
    > values[1] = "one";
    > values[2] = "two";
    > values[3] = "three";
    > values[4] = "four";
    > values[5] = "five";
    >
    > Iterator first = values.find(4);
    > Iterator second = values.find(2);
    >
    > std::cout << "The distance is " << std::distance(first, second) <<
    > ".\n";
    >
    > return 0;
    > }
    >
    > I did not find any documentation that would indicate this should not
    > work.
    > How valid is the use of distance with an SLT map?


    Like other standard algorithms, distance has no knowledge of containers. It
    works with a pair of iterators. What your usage fails to meet is the
    requirement that the second iterator must be reachable from the first
    iterator.

    Ali
    --
    Plug: ACCU's Silicon Valley Chapter meets on second Tuesdays. The meetings
    are open to public and free of charge. Please come tonight for a talk on
    Ada:

    http://accu-usa.org/index.html
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 13, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    574
    klaus hoffmann
    Feb 22, 2006
  2. John
    Replies:
    19
    Views:
    1,139
    Richard Smith
    Mar 13, 2006
  3. kl
    Replies:
    7
    Views:
    1,345
    James Kanze
    Jan 1, 2008
  4. Benny
    Replies:
    2
    Views:
    163
  5. Marc Hoeppner

    How to draw a distance map with Ruby

    Marc Hoeppner, Jun 2, 2008, in forum: Ruby
    Replies:
    11
    Views:
    237
    joseph collins
    Jun 3, 2008
Loading...

Share This Page