473,796 Members | 2,704 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterato r 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(f irst, 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 4735
tr*********@ver izon.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*********@ve rizon.net> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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::iterato r 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(f irst, 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
3906
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 are pre-calculated and passed to the query. SELECT * FROM Locations
12
372
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; however, it won't link during a compile using VC++. The error message I get is " LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
28
1993
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 (int, char); I've written the constructor in the .cpp implementation file as: Distance :: Distance ( int n, char m) : nu(n), me(m) {}
10
370
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 problem. I'm trying to write a class (Distance.h & Distance.cpp) that will perform operations on distances. ie compare, add, subtract etc distance values. I'm still in the early stages of developing the class; however, I'm stuck with how best to...
4
1897
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 as a char rather than incorrectly representing it as an int. I've done this because I want to develop the class so that it will be able to convert between values, ie if I add 500 m to 1 km I'd like a correct result given in metres (1500 m in this...
6
2240
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 a function that I can call to "as required by comparison functions" to convert any value to a mm value so that when I'm comparing Distance objects the values will all be the same. The code I've come up with is below; however, it doesn't work...
3
1709
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 iterator. Here's some code (highly reduced of course). The issue is how the return type of std::distance should be written. #include <iostream> #include <cstddef> #include <iterator>
4
3513
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 useless. I've tried using TestOut's support form to submit the problem but each time I submit I get a "ERROR MESSAGE: Could not modify CSV file.". My problem is that after I run a simulation, the simualtion report comes up. One section of this...
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.