展开 std::vector<std::vector<T>> to std::vector<T>

Unroll std::vector<std::vector<T>> to std::vector<T>

本文关键字:gt vector std lt to 展开      更新时间:2023-10-16

我需要在代码中的很多地方将std::vector<std::vector<T>>转换为std::vector<T>(逐行)。我显然知道如何自己实现它,但是在 boost 或 stl 中是否有任何短代码解决方案?(我只能使用C++98)

UPD:我需要一些非常短的解决方案,比如某个函数的单行调用(可能带有 boost lambda),没有循环,所以C++向量向量的元素推送到向量中的解决方案是不可接受的。

UPD2:请不要用循环发布答案,我知道该怎么做。问题是简短的代码方式,而不是想法。

由于它在很多地方,编写自己的小包装函数来将其放在所有这些地方可能是个好主意。包装器内部的逻辑可以根据性能进行自定义,因为它现在位于单个位置。最简单的可能是

inline template <typename T> 
std::vector<T> to_vector(const std::vector<std::vector<T>>& double_vec) {
  std::vector<T> output;
  for(std::vector<std::vector<T>>::size_type i=0; i < double_vec.size(); ++i) {
    output.insert(output.end(), double_vec[i].begin(), double_vec[i].end());
  }
  return output;
}

如有必要,可以自定义/优化。

没有循环就没有办法做到这一点,事实上,这个任务几乎是使用循环的典型代表。好吧,如果我们迂腐,嵌套循环。

你可以做的是隐藏你通过使用更高级别的结构进行循环的事实,但底层实现将是一组嵌套循环。

template <typename T>
struct flatten_functor
{
   flatten_functor(std::vector<T> &out)
     : m_out(out)
   {}
   bool operator()(std::vector<T> const& to_append)
   {
      m_out.insert(m_out.end(), to_append.begin(), to_append.end());
      return true;
   }
private:
  std::vector<T>& m_out;
};
template <typename T>
std::vector<T> flatten_vector(std::vector<std::vector<T> > const &in)
{
  std::vector<T> retval;
  flatten_functor<T> flatten_this(retval);
  std::for_each(in.begin(), in.end(), flatten_this);
  return retval;
}

如您所见,为了不真正隐藏您正在处理循环的事实,付出了很多努力。我不认为它比循环更具可读性,但话又说回来,这些天我大部分时间都在使用 C++11,使用 lambda 清理这段代码更容易。