【问题标题】:Assigning multi-dimensional vectors with different types分配不同类型的多维向量
【发布时间】:2023-03-28 09:33:01
【问题描述】:

假设我有一个std::vector<std::vector<double>> d 并想将它分配给std::vector<std::vector<int>> i;我能想到的最好的办法是:

#include <vector>
#include <algorithm>

using namespace std;

int main() {
    vector<vector<double>> d = { {1.0, 2.0}, {3.0, 4.0} };
    vector<vector<int>>    i;

    for_each(begin(d), end(d), [&i](vector<double> &x) {
            i.emplace_back(begin(x), end(x));
        }
    );

    return 0;
}

如果两个向量在内部使用相同的类型,我可以只使用赋值运算符(参见C++ copying multidimensional vector):

i = d;

如果向量在内部存储不同的类型,但是是一维的,我可以这样做:

i.assign(begin(d), end(d));

这两者的意图都很明显,我认为我的多维方法解决方案并非如此。有没有更好的方法或公认的习语来做到这一点?

【问题讨论】:

    标签: c++ c++11 vector


    【解决方案1】:

    在我看来,您对 2D 矢量的解决方案是一个很好的解决方案。 当您必须复制向量的向量的N维向量时,就会出现问题...

    假设你想要一个函数copy_multi_vec(),它在如下情况下工作

       std::vector<std::vector<std::vector<double>>> vvvd
        { { {1.0, 2.0, 3.0}, { 4.0,  5.0,  6.0} },
          { {7.0, 8.0, 9.0}, {10.0, 11.0, 12.0} } };
    
       std::vector<std::vector<std::vector<int>>> vvvi;
    
       copy_multi_vec(vvvi, vvvd);
    

    在这种情况下,您可以在辅助类中使用部分模板特化;举例

    template <typename T1, typename T2>
    struct cmvH
     { static void func (T1 & v1, T2 const & v2) { v1 = v2; } };
    
    template <typename T1, typename T2>
    struct cmvH<std::vector<T1>, std::vector<T2>>
     {
       static void func (std::vector<T1> & v1, std::vector<T2> const & v2)
        {
          v1.resize( v2.size() );
    
          std::size_t i { 0U };
    
          for ( auto const & e2 : v2 )
             cmvH<T1, T2>::func(v1[i++], e2);
        }
     };
    
    template <typename T1, typename T2>
    void copy_multi_vec (T1 & v1, T2 const & v2)
     { cmvH<T1, T2>::func(v1, v2); }
    

    或者,如果你想在最后一层使用assign()方法,你可以定义如下的辅助结构

    template <typename, typename>
    struct cmvH;
    
    template <typename T1, typename T2>
    struct cmvH<std::vector<T1>, std::vector<T2>>
     {
       static void func (std::vector<T1> & v1, std::vector<T2> const & v2)
        {
          v1.resize( v2.size() );
          v1.assign( v2.cbegin(), v2.cend() );
        }
     };
    
    template <typename T1, typename T2>
    struct cmvH<std::vector<std::vector<T1>>, std::vector<std::vector<T2>>>
     {
       static void func (std::vector<std::vector<T1>>       & v1,
                         std::vector<std::vector<T2>> const & v2)
        {
          v1.resize( v2.size() );
    
          std::size_t i { 0U };
    
          for ( auto const & e2 : v2 )
             cmvH0<std::vector<T1>, std::vector<T2>>::func(v1[i++], e2);
        }
     };
    

    【讨论】:

      【解决方案2】:

      有没有更好的方法或公认的习语来做到这一点?

      一次分配一个数组元素是不可避免的。你能做的最好的就是创建一个函数来帮助它。

      例如,您可以使用:

      template <typename T1, typename T2>
      void vector_copy(std::vector<std::vector<T1>>& dest,
                       std::vector<std::vector<T2>> const& src)
      {
         // Add the code to do the copy
      }
      

      然后,使用

      vector_copy(d, i);
      

      【讨论】:

        猜你喜欢
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 2018-05-05
        • 2011-02-07
        • 2021-05-20
        • 1970-01-01
        相关资源
        最近更新 更多