Topic: switch operator for string?
Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/01/28 Raw View
Hi,
Maybe is there switch operator for string?
//--------
string s1;
// do something
switch (s1)
{
case "AAAA" :
// do something
break;
case "BBBB" :
// do something
break;
default :
// do something
}
//--------
Thanks in advance
Alex
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/01/29 Raw View
In article <36B03B8C.905695A7@tibam.elex.co.il>, Alex Vinokur <alexander
.vinokur@telrad.co.il> writes
>Hi,
>
>Maybe is there switch operator for string?
No.
Francis Glassborow Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: AllanW@my-dejanews.com
Date: 1999/01/29 Raw View
In article <36B03B8C.905695A7@tibam.elex.co.il>,
Alex Vinokur <alexander.vinokur@telrad.co.il> wrote:
>
> Hi,
Hi!
> Maybe is there switch operator for string?
The switch statement (it is not an operator) requires an integer
type such as int or unsigned short, an enumeration type, or some
class that can be converted in to one of these. Sorry.
You can write this:
if (s1 == "AAAA") { // case "AAAA"
// do something;
} else if (s1 == "BBBB") // case "BBBB"
// do something;
} else { // default
// do something;
}
Make the tests in order with the most common test first, the second
most often test second, and so on. For instance, if s1 will be
"BBBB" more often than it is "AAAA", check for "BBBB" first. If you
do this right, your code will execute as fast -- or faster! -- than
the compiler could have done if switch() had been legal for strings.
----
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]