使用外部条件修改序列操作

Modifying sequence operations with external condition

本文关键字:操作 修改 条件 外部      更新时间:2023-10-16

我经常发现自己在处理一些STL容器,并希望根据一些外部条件对其进行修改。我所说的外部,是指不能单独从容器中的对象派生的东西。

例如,假设我已经根据一些详细的标准计算出了要保留容器中的哪些元素,这些标准不仅涉及元素本身。keep标志存储在与原始容器大小相同的容器中。现在我想使用std::remove_if来删除那些标志为零的对象。我该怎么做?

std::vector<Foo> container_of_foos;
std::vector<int> to_keep(container_of_foos.size(), 0); 
// ... code calculates which foos to keep and stores a flag for each one
// NOTE: the condition relies information external to the Foo class (could be relation to other Foo instances)
auto i_new_end = std::remove_if(begin(container_of_foos), end(container_of_foos), [&to_keep](const Foo& foo) {
    // can't tell whether to keep, because I don't know which object is iterated now
});

使用boost::zip_iterator

一组元组迭代器上的boost::zip_iterator确实简化了删除。

std::vector<int> numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<bool> selectors = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
auto zip_first = boost::make_zip_iterator(
    boost::make_tuple(selectors.begin(), numbers.begin()));
auto zip_last = zip_first + std::min(selectors.size(), numbers.size());
auto removed_first = std::remove_if(zip_first, zip_last, [](const auto& tup) {
  return boost::get<0>(tup) == 0;
});

Wandbox上的实时示例。

使用算法(非提升)

编写自己的remove/remove_if,它接受两个范围(值和选择器)和一个值/谓词。您必须决定如何处理不匹配的测距。一旦有两个范围处于预期状态,就可以通过擦除来运行压缩。以下是压缩一个范围的示例,该范围使用另一个范围进行过滤,以最短序列结束。

按值和谓词删除

template <typename FwdIt1, typename FwdIt2, typename ValueType>
auto remove(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2,
            const ValueType value) {
  FwdIt1 curr1 = first1;
  FwdIt2 curr2 = first2;
  for (; curr1 != last1 && curr2 != last2; ++curr1, ++curr2) {
    if (value != *curr2) {
      *first1++ = std::move(*curr1);
      *first2++ = std::move(*curr2);
    }
  }
  return std::make_pair(first1, first2);
}
template <typename FwdIt1, typename FwdIt2, typename Predicate>
auto remove_if(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2,
               Predicate pred) {
  FwdIt1 curr1 = first1;
  FwdIt2 curr2 = first2;
  for (; curr1 != last1 && curr2 != last2; ++curr1, ++curr2) {
    if (!pred(*curr2)) {
      *first1++ = std::move(*curr1);
      *first2++ = std::move(*curr2);
    }
  }
  return std::make_pair(first1, first2);
}

基于容器的压缩(删除和擦除)助手

template <typename Container, typename Selector, typename ValueType>
auto compress(Container& values, Selector& selectors, const ValueType& value) {
  const auto remove_iters =
      remove(std::begin(values), std::end(values), std::begin(selectors),
             std::end(selectors), value);
  return std::make_pair(
      values.erase(remove_iters.first, std::end(values)),
      selectors.erase(remove_iters.second, std::end(selectors)));
}
template <typename Container, typename Selector, typename Predicate>
auto compress_if(Container& values, Selector& selectors, Predicate pred) {
  const auto remove_iters =
      remove_if(std::begin(values), std::end(values), std::begin(selectors),
                std::end(selectors), pred);
  return std::make_pair(
      values.erase(remove_iters.first, std::end(values)),
      selectors.erase(remove_iters.second, std::end(selectors)));
}

Wandbox上的实时示例。

在C++11中,使用lambda作为谓词。例如

void func()
{
     std::vector<int> container;
      // populate container
     int flag = 42;
      //  modify flag as needed
      auto lambda = [flag](int element) {return element < flag;}  // whatever
     std::remove_if(container.begin(), container.end(), lambda);
}

在C++11之前,请使用函子。

struct remover
{
     int flag;
     remover(int flag_value) : flag(flag_value) {};
     bool operator()(int element) {return element < flag;};
};
void func()
{
     std::vector<int> container;
      // populate container
     int flag = 42;
      //  modify flag as needed

     remover functor(flag);
     std::remove_if(container.begin(), container.end(), functor);
}

这两个样本本质上是等价的。

在C++11中,查找lambda捕获规范,找出如何将其他变量传递给lambda。在此之前,请更改函子,使其使用所需的任何变量进行构造。

&x-vec.data()vecx的索引。

当然,假设xvec中:否则为UB。