Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with distance and STL map

Reply
Thread Tools

Problem with distance and STL map

 
 
tron.thomas@verizon.net
Guest
Posts: n/a
 
      09-13-2005
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?

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      09-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      09-13-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL map or hash map using struct as data and find it kl C++ 7 01-01-2008 11:05 AM
a stl map which use stl pair as the key Allerdyce.John@gmail.com C++ 2 02-22-2006 07:25 AM
STL: Map of maps possible, but no multi-map of maps? Workarounds? Marcus C++ 2 12-09-2005 06:34 AM
USA time distance and map search script Benny ASP General 2 06-03-2005 05:10 PM
[Q]Floating static routes, admin. distance and route-map Castravete Cisco 0 03-10-2005 02:59 PM



Advertisments