【问题标题】:Search for multiple strings at once一次搜索多个字符串
【发布时间】:2012-03-30 18:33:59
【问题描述】:

我想一次在一个向量中搜索多个字符串。

即向量 = "H" "H" "I" "I" vector2 = "H" "I"

所以我想用 vector2 的内容搜索向量,我的代码在下面,但我认为这不是最好的方法。如果所有字符串都存在,则返回一个标识符,以便我知道所有字符串都存在。

有人可以检查下面的代码,看看它是否正确 :) 谢谢

std::vector<std::string> test; 
        test.push_back("YES");
        test.push_back("YES");
        test.push_back("NO");
        test.push_back("NO");

        std::vector<std::string> test1; 
        test1.push_back("YES");
        test1.push_back("NO");

        std::vector<std::string>::iterator it;
        for(int i = 0; i < test1.size(); i++)
        {



            if(find (test.begin(), test.end(),test[i]) != test.begin() )
            {
                DCS_LOG_DEBUG("Some elements have appeared more than once...");

            }

        }

【问题讨论】:

标签: c++ search vector find


【解决方案1】:

我也需要类似的东西,所以写了几行对我来说非常好用。

    // allCollectableItems is a vector<string> containing all strings to be searched
    // itemsCollected is a vector<string> in which allCollectableItems are to be looked for

    bool allItemsFound = true;
    
                    for (string item : allCollectableItems)
                    {
                        if (std::find(itemsCollected.begin(), itemsCollected.end(), item) == itemsCollected.end())
                        {
                            allItemsFound = false;
                            break;
                        }
                    }

// allItemsFound will be the required value

【讨论】:

    【解决方案2】:

    让我添加一个不包含显式 for 的解决方案,并假设:

    • test1中没有重复项。
    • 您正在寻找要包含在测试中的完全相同的字符串。

      #include <vector>
      #include <string>
      #include <algorithm>
      #include <iostream>
      
      using namespace std;
      
      struct StringFoundCounter 
      {
          StringFoundCounter(const vector<string>& haystack) 
              : sum(0), haystack(haystack) { }
      
          void operator()(string needle) {
              if (find(haystack.begin(), haystack.end(), needle) != haystack.end())
                  sum++;
          }
      
          int get_sum() const  { return sum; }
      
      private:
          int sum;
          const vector<string>& haystack;
      };
      
      int main()
      {
          std::vector<std::string> test;
          test.push_back("YES");
          test.push_back("YES");
          test.push_back("NO");
          test.push_back("NO");
      
          std::vector<std::string> test1;
          test1.push_back("YES");
          test1.push_back("NO");
          test1.push_back("CR");
      
          StringFoundCounter sfc = 
              for_each(test1.begin(), test1.end(), StringFoundCounter(test));
      
          if (test1.size() == sfc.get_sum())
              cout << "All elements found" << endl;
          else
              cout << "Some or all elements not found" << endl;
      }
      

      ~

    【讨论】:

      【解决方案3】:

      如果可以对两个向量进行排序,可以使用std::includes

      【讨论】:

        【解决方案4】:

        问题可能是:

        if(find (test.begin(), test.end(),test[i]) != test.begin() )
        

        而不是

        if(find (test.begin(), test.end(),test1[i]) != test.end() )
        

        您的版本在test 中查找test 的元素,这将始终返回一个有效的迭代器。

        除此之外,当你找到一个不存在的string时,你可以从循环中直接break,不需要继续搜索,对吧?

        【讨论】:

        • 是的,这是正确的,我有一个字符串向量,我将用字符串填充另一个向量,我必须检查第一个向量 test 是否包含来自 的那些字符串测试1 @LuchianGrigore
        • @ShamariCampbell 好的,那么应该这样做。
        • 哦是的我的意思是把 test1 对不起@LuchianGrigore
        • 但是,如果我将 else 和 break 放在 else 内,它将无法正常工作,因为如果 strings 存在 @LuchianGrigore ,它也会进入 else
        • @ShamariCampbell 你还需要end 而不是begin。如果在向量中找到字符串,它只会进入else。否则,它将从循环中中断。如果你闯入if,它就不会进入else
        【解决方案5】:

        比较不正确。如果您想检查容器中是否有 至少一个 元素,而不是:

        if(find(test.begin(), test.end(),test[i]) != test.begin())
        

        你应该使用:

        if(find(test.begin(), test.end(),test1[i]) != test.end())
        

        because find returns test.end() 找不到匹配项。

        如果您想检查是否存在多个元素,请使用count:

        if(count(test.begin(), test.end(),test1[i]) > 1)
        

        【讨论】:

        • 不,我不需要检查是否存在多个元素,只需要找出是否存在,如果不存在则退出循环并发出错误信号@RafalRawicki
        • 我想我可以检索每个的计数并检查它们是否等于一个 @RafalRawicki
        • 日志消息“一些元素出现了不止一次......”我不清楚。无论如何,与test.begin() 相比没有意义。
        • 日志消息只是说有些元素不应该出现多次
        • 我现在正在使用 if count 但是当我输入 else 时它会同时执行,嗯,但我不需要 else 在这里我可以只在 if 中使用 break但只是说
        猜你喜欢
        • 2013-12-24
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        相关资源
        最近更新 更多