English 中文(简体)
Variadic string-comparison
原标题:Variadic string-comparison

我正在与一些旧的法典(正好发生的文本游戏)合作,希望取代模式。

strcasecmp(variable, "something") == 0 || strcasecmp(variable, "something else") == 0

with ,

in_list(variable, "something", "something else")

我认为,真正的职能是适当的。 但是,当我看一页时,我就看到,在你离开论点时,没有任何东西可以说出(请打电话va_arg)。 当你产生未界定的行为时。 因此,我如何处理这一问题?

或许可以采取某种方式来绕过这一限制。 Maybe I can #define 在名单结束时,有些派系,因此,我可以检查,尽管这似乎不合法。 我认为我只能用1,2的宏观家庭来取代它,......提出某种合理限制的论点,尽管这感觉是一种ck。

这样做的正确途径是什么? Suppose I m不愿重写节目以使用string 类型,而且I m 困于char*

最佳回答

Assuming you can use C++11 features, I d have the function take a collection type (e.g., std::set) that supports initializer lists, so you could use something like:

in_list(variable, {"something", "something else", "yet a third thing"});

Edit: 这里是快速的:

#include <string>
#include <set>
#include <iostream>

bool in_list(std::string const &value, std::set<std::string> const &list) {
    return list.find(value) != list.end();
}

int main(){
    std::cout << std::boolalpha << in_list("true", {"this", "is", "a", "true", "statement"}) << "
";

    std::cout << in_list("false", {"this", "is", "a", "true", "statement"});
    return 0;
}

该表以4.7.0g++进行清点汇编,并产生预期产出:

true
false

是的,如果没有其他理由,std:set 这将是对手头工作的合理选择。 关于您对<代码>char* 诉std:string :string支持从char *中进行默示转换,因此,您可通过char *至该功能(如上表所示),其编号可改为。 换言之,(多数)其他法典只能通过<条码>char *,而不是担心该法典将其视为<条码>的细微细节:载<>。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签