473,396 Members | 1,816 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,396 software developers and data experts.

std::map Help

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey all. I've got a problem I can't seem to figure out.

I'm writing a program that deals with money in multiple currencies. In
order to define the various currencies, I've created a class called
Currency. I then have a map that looks like this:

std::map<std::string, Currency> currency;

The key to this array is the abbreviation of the currency used, i.e.:
"CAD" = Canadian Dollar, "USD" = U.S. Dollar, etc.

All the data gets loaded from a file. Is there any way to check if a
given key exists within a map so that I could prevent a corrupted file
from creating a segmentation fault by asking for a currency that it
hasn't defined?

- --
Regards,
Jonathan Lamothe

/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/

die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEXLntNrv4JaRC3JsRAqdCAKDFSV7DmWVlPBryupJJpy M3pZcCEgCggsJ2
h+8sjJT1T2iyopOTL/hh1Vs=
=Fmeo
-----END PGP SIGNATURE-----
May 6 '06 #1
3 6102
In article <SR*******************@news20.bellglobal.com>,
jo**************@hotmail.com says...

[ ... ]
Is there any way to check if a
given key exists within a map so that I could prevent a corrupted file
from creating a segmentation fault by asking for a currency that it
hasn't defined?


map::find() returns an iterator equal to your_map.end()
if the key you searched for isn't present in the map.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 6 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jerry Coffin wrote:
In article <SR*******************@news20.bellglobal.com>,
jo**************@hotmail.com says...

[ ... ]

Is there any way to check if a
given key exists within a map so that I could prevent a corrupted file
from creating a segmentation fault by asking for a currency that it
hasn't defined?

map::find() returns an iterator equal to your_map.end()
if the key you searched for isn't present in the map.


Thanks. I had a feeling it had something to do with find, but the
documentation I was looking at wasn't all that clear.

- --
Regards,
Jonathan Lamothe

/*
* Oops. The kernel tried to access some bad page. We'll have to
* terminate things with extreme prejudice.
*/

die_if_kernel("Oops", regs, error_code);
-- From linux/arch/i386/mm/fault.c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEXM2cNrv4JaRC3JsRAjRpAKCIae/hiDZH3i0kPLH0V9WnRjJLEgCfZQpI
wvp4jfYn8gTCvm88lo0xiKw=
=aWn4
-----END PGP SIGNATURE-----
May 6 '06 #3
In article <C4*******************@news20.bellglobal.com>,
jo**************@hotmail.com says...

[ ... ]
Thanks. I had a feeling it had something to do with find, but the
documentation I was looking at wasn't all that clear.


Dinkumware has a standard library reference online at:
http://www.dinkumware.com/refxcpp.html

On paper, I recommend _The C++ Stanard Library_ by
Josuttis. For the iterators/containers/algorithms part of
the library, _STL Tutorial and Reference Guide_ by Saini
and Musser is quite good as well (if memory serves, the
current edition has a slightly different name and a third
author, but I'm feeling a bit too lazy to look it up
right now...)

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 6 '06 #4

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

Similar topics

1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
by: ash | last post by:
Hi, I'm getting started with STL, and am stuck at creating a map container. I checked one of the texts and found a code in there. To make it simple, i wrote the following: #include...
5
by: EnTn | last post by:
Hi Everyone... I've been trying to use a std::map to do some storage. Basically, i'm storing double values using a Key Object. The Key object is quite simple and is just a pair of int's...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003)...
2
by: Fei Liu | last post by:
Hello, I am getting something really wierd out of this source code, can you please verify if you also get segmentation fault (memory failure)? http_form.h: (sorry about the line wrapping, email...
4
by: Evyn | last post by:
Hi all, I'm starting to fool around with STL and in particular std::map. How do I iterate through one map and insert every pair in another map? I have the following so far: map<double,...
7
by: DevNull | last post by:
Hi there everyone, I'm creating a very simple immediate mode command interpreter. The final purpose is to provide a pluggable control and command console for a MUD server I have written. The...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.