比较更多50个字符串的有效方法

时间:2019-12-04 13:59:03

标签: c++ string c++11

我有一个方法,它采用两个参数,一个为string,另一个为int

该字符串必须与 50个字符串进行比较,一旦找到匹配项,则需要使用硬编码字符串映射int值,如下例所示

EX:

  string Compare_Method(std::string str, int val) {

     if(str == "FIRST")
{
  std::array<std::string, 3> real_value = {"Hello1","hai1","bye1"}
  return real_value[val];
}

     else if(str == "SECOND")
{
  std::array<std::string, 4> real_value = {"Hello2","hai2","bye2"}
  return real_value[val];
}

     else if(str == "THIRD")
{
  std::array<std::string, 5> real_value = {"Hello3","hai3","bye3"}
  return real_value[val];
}

//----- 50+ else if

}

我的方法如上所述。什么是有效的方法

1。要比较50个以上的字符串。

2。为每个if情况创建std :: array

已编辑:std :: array的大小不固定,可以是上面编辑的3、4、5。

3 个答案:

答案 0 :(得分:9)

这就是我的方法。数据结构只创建一次,访问时间应该足够快

tf.control_dependencies

如果#include <iostream> #include <string> #include <array> #include <unordered_map> std::string Compare_Method(const std::string& str, int val) { // or std::vector<std::string> static std::unordered_map<std::string, std::array<std::string, 3>> map { { "FIRST", { "Hello1", "hail1", "bye1" }}, { "SECOND", { "Hello2", "hail2", "bye2" }}, { "THIRD", { "Hello3", "hail3", "bye3" }}, // 50+ more }; // maybe check if str is present in the map return map[str][val]; } int main() { std::cout << Compare_Method("SECOND", 1) << std::endl; } 不够(快速),您可以想出某种静态的最佳哈希结构,因为键是在编译时知道的。

答案 1 :(得分:2)

如果这50个字符串是您在整个程序中广泛使用的字符串,则string比较会降低性能。我建议您使它们适应enum

enum Strings
{
    FIRST,
    SECOND,
    THIRD,
    …
    …
}

您显然需要一种方法,只要从源中获取stringint(用户输入,文件读取等)即可。这应该尽可能少用,因为您的系统现在可以使用枚举值(可以将其用作STL容器的索引,如我们在下一步中看到的那样)

int GetEnumIndex(const std::string& str)
{
    // here you can map all variants of the same string to the same number
    if ("FIRST" == str || "first" == str) return 1;
    …
}

然后,比较方法可以基于enum而不是string

std::string Compare_Method(const int& strIndex, int val)
{
    static std::vector<std::vector<std::string>> stringArray
    {
        { "Hello1", "hail1", "bye1" },
        { "Hello2", "hail2", "bye2", "aloha2" },
        { "Hello3", "hail3", "bye3", "aloha3", "tata3" },
        …
    };
    return stringArray[strIndex][val];
}

答案 2 :(得分:2)

根据您提供的信息,我尝试了各种变体,以找到实现目标的最佳方法。我在这里列出了最好的一个。您可以查看其他方法here

您可以编译它并运行run.sh来比较所有情况下的性能。

std::string Method6(const std::string &str, int val) {
  static std::array<std::string, 5> NUMBERS{"FIRST", "SECOND", "THIRD",
                                            "FOURTH", "FIFTH"};
  static std::array<std::vector<std::string>, 5> VALUES{
      std::vector<std::string>{"FIRST", "hai1", "bye1"},
      std::vector<std::string>{"Hello1", "SECOND", "bye1"},
      std::vector<std::string>{"Hello1", "hai1", "THIRD"},
      std::vector<std::string>{"FOURTH", "hai1", "bye1"},
      std::vector<std::string>{"Hello1", "FIFTH", "bye1"}};
  for (int i = 0; i < NUMBERS.size(); ++i) {
    if (NUMBERS[i] == str) {
      return VALUES[i][val];
    }
  }
  return "";
}

为简单起见,我一直在使用NUMBERS,长度为5,但是您可以使用任意长度。

VALUESstd::array中的std::vector,因此您可以将任何数字if元素添加到std::vector

  github代码的

输出。

Method1   880
Method2   851
Method3   7292
Method4   989
Method5   598
Method6   440

根据您的系统和执行时的系统负载,您的输出可能会有所不同。

相关问题