| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Jonathan Lamothe" |
| Date: |
06 May 2006 09:59:57 AM |
| Object: |
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
iD8DBQFEXLntNrv4JaRC3JsRAqdCAKDFSV7DmWVlPBryupJJpyM3pZcCEgCggsJ2
h+8sjJT1T2iyopOTL/hh1Vs=
=Fmeo
-----END PGP SIGNATURE-----
.
|
|
| User: "Jerry Coffin" |
|
| Title: Re: std::map Help |
06 May 2006 11:13:08 AM |
|
|
In article <SR27g.4792$ix6.405180@news20.bellglobal.com>,
jonathan_lamothe@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.
.
|
|
|
| User: "Jonathan Lamothe" |
|
| Title: Re: std::map Help |
06 May 2006 11:23:56 AM |
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jerry Coffin wrote:
In article <SR27g.4792$ix6.405180@news20.bellglobal.com>,
jonathan_lamothe@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-----
.
|
|
|
| User: "Jerry Coffin" |
|
| Title: Re: std::map Help |
06 May 2006 11:52:04 AM |
|
|
In article <C447g.2760$VV2.207071@news20.bellglobal.com>,
jonathan_lamothe@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.
.
|
|
|
|
|
|

|
Related Articles |
|
|