将字符串变量与一组字符串常量进行比较的最佳方法是什么?

发布于 2024-08-02 02:36:20 字数 72 浏览 3 评论 0原文

if 语句看起来太尴尬了,因为我需要增加常量数量的可能性。 很抱歉让您误以为“常数”而不是我的意思。

if statement looks too awkward, because i need a possibility to increase the number of constatnts.
Sorry for leading you into delusion by that "constant" instead of what i meant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

就此别过 2024-08-09 02:36:20

将所有常量添加到 std::set 然后您可以检查该集合是否包含您的字符串

std::set<std::string> myLookup;
//populate the set with your strings here

set<std::string>::size_type i;

i = myLookup.count(searchTerm);
if( i )
    std::cout << "Found";
else 
    std::cout << "Not found";

Add all your constants to a std::set then you can check if the set contains your string with

std::set<std::string> myLookup;
//populate the set with your strings here

set<std::string>::size_type i;

i = myLookup.count(searchTerm);
if( i )
    std::cout << "Found";
else 
    std::cout << "Not found";
戈亓 2024-08-09 02:36:20

取决于你是否关心性能。

如果没有,那么最简单的代码可能是将各种字符串放入数组中(如果您想在运行时增加常量的数量,则将其放入向量中)。 对于少量字符串来说,这也将相当快:

static const char *const strings[] = { "fee", "fie", "fo", "fum" };
static const int num_strings = sizeof(strings) / sizeof(char*);

然后:

int main() {
    const char *search = "foe";
    bool match = false;
    for (int i = 0; i < num_strings; ++i) {
        if (std::strcmp(search, strings[i]) == 0) match = true;
    }
}

或者:

struct stringequal {
    const char *const lhs;
    stringequal(const char *l) : lhs(l) {}
    bool operator()(const char *rhs) {
        return std::strcmp(lhs, rhs) == 0;
    }
};

int main() {
    const char *search = "foe";
    std::find_if(strings, strings+num_strings, stringequal(search));
}

[警告:我还没有测试上面的代码,并且我已经多次得到签名错误......]

如果你确实关心性能,并且有合理数量的字符串,那么一个快速选择就是 Trie。 但这需要付出很大的努力,因为标准 C++ 库中没有这样的库。 获得很多好处。

// These strings MUST be in ASCII-alphabetical order. Don't add "foo" to the end!
static const char *const strings[] = { "fee", "fie", "fo", "fum" };
static const int num_strings = sizeof(strings) / sizeof(char*);

bool stringcompare(const char *lhs, const char *rhs) {
    return std::strcmp(lhs, rhs) < 0;
}

std::binary_search(strings, strings+num_strings, "foe", stringcompare);

您可以使用排序的数组/向量、使用 std::binary_search: ... 进行搜索或使用 std::set 来 但是,除非您在运行时更改字符串集,否则使用二分搜索的集合比使用排序数组没有任何优势,并且必须用代码填充集合(或向量),而数组可以静态初始化。 我认为 C++0x 会通过集合的初始值设定项列表来改进一些事情。

Depends whether you care about performance.

If not, then the simplest code is probably to put the various strings in an array (or vector if you mean you want to increase the number of constants at run time). This will also be pretty fast for a small number of strings:

static const char *const strings[] = { "fee", "fie", "fo", "fum" };
static const int num_strings = sizeof(strings) / sizeof(char*);

Then either:

int main() {
    const char *search = "foe";
    bool match = false;
    for (int i = 0; i < num_strings; ++i) {
        if (std::strcmp(search, strings[i]) == 0) match = true;
    }
}

Or:

struct stringequal {
    const char *const lhs;
    stringequal(const char *l) : lhs(l) {}
    bool operator()(const char *rhs) {
        return std::strcmp(lhs, rhs) == 0;
    }
};

int main() {
    const char *search = "foe";
    std::find_if(strings, strings+num_strings, stringequal(search));
}

[Warning: I haven't tested the above code, and I've got the signatures wrong several times already...]

If you do care about performance, and there are a reasonable number of strings, then one quick option would be something like a Trie. But that's a lot of effort since there isn't one in the standard C++ library. You can get much of the benefit either using a sorted array/vector, searched with std::binary_search:

// These strings MUST be in ASCII-alphabetical order. Don't add "foo" to the end!
static const char *const strings[] = { "fee", "fie", "fo", "fum" };
static const int num_strings = sizeof(strings) / sizeof(char*);

bool stringcompare(const char *lhs, const char *rhs) {
    return std::strcmp(lhs, rhs) < 0;
}

std::binary_search(strings, strings+num_strings, "foe", stringcompare);

... or use a std::set. But unless you're changing the set of strings at runtime, there is no advantage to using a set over a sorted array with binary search, and a set (or vector) has to be filled in with code whereas an array can be statically initialized. I think C++0x will improve things, with initializer lists for collections.

伊面 2024-08-09 02:36:20

将要比较的字符串放入静态向量或集合中,然后使用 std::find 算法。

Put the strings to be compared in a static vector or set and then use std::find algorithm.

夜深人未静 2024-08-09 02:36:20

技术上最好的解决方案是:根据您的字符串常量集构建一个“完美的哈希函数”,这样以后在哈希过程中就不会发生冲突。

The technically best solution is: build a 'perfect hash function' tailored to your set of string constants, so later there are no collisions during hashing.

下壹個目標 2024-08-09 02:36:20
const char * values[]= { "foo", "bar", ..., 0 };

bool IsValue( const std::string & s ) {
   int i = 0;
   while( values[i] ) {
      if ( s == values[i] ) {
         return true;
      }
      i++;
   }
   return false;
}

或者使用 std::set。

const char * values[]= { "foo", "bar", ..., 0 };

bool IsValue( const std::string & s ) {
   int i = 0;
   while( values[i] ) {
      if ( s == values[i] ) {
         return true;
      }
      i++;
   }
   return false;
}

Or use a std::set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文