Re: What is the output of this program?
Seungbeom Kim wrote:
derek@antiquark.com wrote:
Probably the easiest change would be to use the string's "at" function
for range-checked access, and encase your code in a try/catch block to
print an error message.
Also, you could use "vector<string>" instead of "string v1[5]". vector
also provides a at() function, but you usually have to initialize it
with a bunch of .push_back's.
Is .at() intended for debugging?
I don't think so. On the other hand, if he had used at()
instead of just [], he would have gotten a fatal error, unless
someone, somewhere trapped the exception and silenced it. But a
hard error is certainly preferable.
If the OP had known that he/she could have used .at(), he/she
wouldn't have gotten into the problem at first. And even
after the bug is gone, he/she would have to keep suffering the
additional cost of range checking.
The additional cost is peanuts. The standard solution, using
push_back, is probably more expensive. But neither are
(normally) expensive enough to worry about.
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]