473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with distance and STL map

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 '05 #1
2 4716
tr*********@verizon.net 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)
Sep 13 '05 #2
<tr*********@verizon.net> wrote in message
news:11*********************@o13g2000cwo.googlegro 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
Sep 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
12
by: Chiller | last post by:
I'm in the early stages of developing a class that will represent a metric distance by storing both a number and unit (ie KM, M, CM etc). I've developed some initial code as a starting point;...
28
by: Chiller | last post by:
I am in the process of writing a class that will represent metric distances by accepting a value (ie, 3) and a unit of measure (ie, m). I've written my constructor in the .h file as Distance...
10
by: Chiller | last post by:
Ok, this is a continuation of a problem I posted on an earlier thread. I've started another thread because my problem has progressed from the initial constructor problem into a general method...
4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
6
by: Chiller | last post by:
I'm in the process of writing a class that performs functions on a Distance object. The object is created by entering details as "Distance a (5, km)" or "Distance b (3, cm)" etc. I wish to write...
3
by: John Harrison | last post by:
This might be a compiler problem or it might be some subtlety of the language I don't understand. I'm writing an iterator and I want to write a version of std::distance specially for my...
4
by: PSL | last post by:
Hi, I am going through the training C# For Programmers made by TestOut and like the presentation. The only problem is that this tutorial has a nastie bug that renders this software practically...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.