简体   繁体   English

C ++中的中间值

[英]Intermediate values in C++

I can not find how to implement a design in C++. 我找不到如何在C ++中实现设计。 In the language of Delphi in case the operator can write the following design: 在Delphi的语言中,如果操作员可以编写以下设计:

    case s[j] of
         '0'..'9','A'..'Z','a'..'z','_': doSomeThing();

How can i do the same in c++. 我怎样才能在c ++中做同样的事情。 Attracts me is the construction type 'a' .. 'z' and etc... 吸引我的是建筑类型'a'..'z'等...

Thank you 谢谢

您可以使用isalnum函数执行此操作:

if(isalnum(s[j]) || (s[j] == '_') )

You wouldn't use a switch/case statement for this in C++. 你不会在C ++中使用switch / case语句。 C++ also provides pre-built functions to test most of that, so you'd use something like: C ++还提供预构建的函数来测试大部分内容,因此您可以使用以下内容:

if (isalnum(s[j]) || s[j]=='_')
    doSomething();

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-else-if如果你需要范围:

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 can try this too: 你也可以试试这个:

const std::string char_set("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
if (char_set.find(s[j] != std::string::npos)
{
    doSomething();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM