std::map Help

  • Thread starter Jonathan Lamothe
  • Start date
J

Jonathan Lamothe

-----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

iD8DBQFEXLntNrv4JaRC3JsRAqdCAKDFSV7DmWVlPBryupJJpyM3pZcCEgCggsJ2
h+8sjJT1T2iyopOTL/hh1Vs=
=Fmeo
-----END PGP SIGNATURE-----
 
J

Jerry Coffin

[ ... ]
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.
 
J

Jonathan Lamothe

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

Jerry said:
[ ... ]

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-----
 
J

Jerry Coffin

[ ... ]
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...)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top