I 开发者_运维技巧can not find how to implement a design in C++. In the language of Delphi in case the operator can write the following design:
case s[j] of
'0'..'9','A'..'Z','a'..'z','_': doSomeThing();
How can i do the same in c++. Attracts me is the construction type 'a' .. 'z' and etc...
Thank you
You can do it using isalnum function as:
if(isalnum(s[j]) || (s[j] == '_') )
The short answer is that it is impossible. You can simulate a list of values like this:
switch (s[j])
{
case '0':
case '1':
case '2':
case '3':
doSomething1();
break;
case 'a':
case 'b':
case 'c':
case 'd':
doSomething2();
break;
}
But you cannot specify ranges. You should use if-else-if if you need ranges:
if ( (s[j] >= '0') && (s[j] <= '9'))
doSomething1();
else if ( (s[j] >= 'a') && (s[j] <= 'z'))
doSomething2();
Anyway, if's are much more safe than switch's :-)
You wouldn't use a switch/case statement for this in C++. C++ also provides pre-built functions to test most of that, so you'd use something like:
if (isalnum(s[j]) || s[j]=='_')
doSomething();
You can try this too:
const std::string char_set("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
if (char_set.find(s[j] != std::string::npos)
{
doSomething();
}
精彩评论