c++ - 如何从字符串 vector c++ 而不是新 vector 中过滤

标签 c++ vector

在 C++ 中,我从一个 vector matchingFirstAndLast 开始,其中填充了符合某些条件(基于 userInput)的单词。平均而言,它非常大,所以我对其应用过滤器并创建一个 vector 来存储过滤后的内容。我觉得那不优雅;我想过滤(如 F# 列表)而不是创建新 vector 。

换句话说,我希望有一个 vector 被反复过滤而不创建新 vector 。

My code's data flow :: matchingFirstAndLast |> sequenced |> appropriateLength |> finalSuggestions`

//我的代码(以备不时之需)

vector<finalwords> compute      (string userInput,
                                 vector<string>dictionary,
                                 vector<string>popular,
                                 vector<string>keyboard_layout)
{
  //matchingFirstAndLast will hold words with the same first and last letter as the input string
  vector<string>matchingFirstAndLast;
  int inputLength = userInput.length();

  //for every word in the big dictionary vector, look for first and last letter similarites
  for (string &x : dictionary)
    if (userInput[0] == x.front() && userInput[inputLength - 1] == x.back())
      matchingFirstAndLast.push_back (x);

  //sequenced will hold words whose letters are found in sequence in the userInput string
  vector<string>sequenced;

  for (string &x : matchingFirstAndLast)
    if (FoundInSequence (userInput, x))
      sequenced.push_back (x);

  //determine the minimum word length based on the number of times the string changes
  //rows on the keyboard.
  int minLength = GetMinWordLength (userInput, keyboard_layout);

  //appropriateLength will hold all words longer than the minLength
  vector<string>appropriateLength;

  for (auto &x : sequenced)
    if (x.length() > minLength || minLength < 0)
      appropriateLength.push_back (x);

  vector<finalwords> finalSuggestions;

  for (string &x : appropriateLength)
    if (find (popular.begin(), popular.end(), x) != popular.end()) //word found in popular.txt
      finalSuggestions.push_back (finalwords (x, true, true, edit_distance (userInput, x)));
    else
      finalSuggestions.push_back (finalwords (x, false, true, edit_distance (userInput, x)));

  //sort the returned vector by most popular first
  sortResults (finalSuggestions);

  return finalSuggestions;
}//end compute(...)

在python中,例如,这是可能的

suggestions = filter(lambda x: x[0] == path[0] and x[-1] == path[-1], WORDS)
suggestions = filter(lambda x: match(path, x), suggestions)
suggestions = filter(lambda x: len(x) > min_length, suggestions)

这不会将“过滤后”的数据存储到新容器中。

像 python 示例 ^,我想要一些在 C++ 中执行此操作的方法

最佳答案

'过滤器'有点模棱两可。从我的角度来看,当你说你试图“过滤”一个 vector 时,对我来说,这意味着你想创建另一个 vector 只有来自原始列表。但是您帖子的文字清楚地表明这不是您想要的。所以我的结论是,您真正想要的是 vector 中元素的选择性迭代。换句话说,您想遍历列表中的元素,但只对其中的一些元素进行操作。

如果是这样,那么我建议使用虚构的 Std Lib 算法 for_each_if。我说是虚构的,因为没有这样的算法,但我以前实现过,并不难。

按照这些思路应该可以做到这一点(未经测试):

template <typename InIt, typename Predicate, typename UnaryFunction>
UnaryFunction for_each_if (InIt first, InIt last, UnaryFunction fn, Predicate pr)
{
  for (; first != last; ++first)
  {
    if (pr (*first))
      fn (*first);
  }
  return fn;
}

使用它类似于使用 std::for_each,不同之处在于您还像使用 copy_if 一样指定谓词。假设使用 C++11,您可以使用 lambda 执行所有这些操作。

关于c++ - 如何从字符串 vector c++ 而不是新 vector 中过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29753518/

相关文章:

c++ - vector 问题,C++ 不确定我做错了什么

c++ - 从 ARM 的源代码交叉编译 Qt 4.7 的问题

c# - 使用 Emgu OpenCV 计算直方图

c++ - 迭代器复制构造函数错误,非法作为右侧 '.' 运算符

c++ - 使用 Int 的初始化列表初始化 Long Double 的 vector

math - 由2个角度定义的3D矢量

c++ - 为什么在使用静态 constexpr 成员构造时 std::make_shared 与 new 不同?

c++ - 将现有项目添加到解决方案编辑项目文件

c++ - 将 std::string 转换为 vector<char>

c++ - 将 vector 的 end() 传递给 std::vector::erase 是否有效?