简体   繁体   English

分配不同类型的多维向量

[英]Assigning multi-dimensional vectors with different types

Suppose I have a std::vector<std::vector<double>> d and want to assign it to a std::vector<std::vector<int>> i ; 假设我有一个std::vector<std::vector<double>> d并想将其分配给std::vector<std::vector<int>> i the best I could come up with was: 我能想到的最好的是:

#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;
}

If both vectors were using the same type internally, I could just use the assignment operator (see C++ copying multidimensional vector ): 如果两个向量内部都使用相同的类型,则可以使用赋值运算符(请参见C ++复制多维向量 ):

i = d;

If the vectors were storing different types internally, but one-dimensional, I could do: 如果向量在内部存储不同类型,但是是一维的,我可以这样做:

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

Both of those are really obvious in their intention, which I don't feel is the case with my solution for the multi-dimensional approach. 两者的意图确实很明显,我对多维方法的解决方案并不这么认为。 Is there a better way, or an accepted idiom, to do this? 是否有更好的方法或公认的习惯用法?

It seems to me that your solution for the 2D vector is a good one. 在我看来,您对于2D向量的解决方案是一个很好的解决方案。 The problem arises when you have to copy a N-dimension vectors of vectors of vectors... 当您必须复制向量的向量的N维向量时,就会出现问题。

Suppose you want a function copy_multi_vec() that works in a case as follows 假设您需要一个在以下情况下起作用的函数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);

In this case you can use partial template specialization in an helper class; 在这种情况下,可以在帮助器类中使用部分模板专门化功能。 by example 举个例子

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); }

or, if you want use the assign() method for the last level, you can define the helper struct as follows 或者,如果您想在最后一级使用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);
    }
 };

Is there a better way, or an accepted idiom, to do this? 是否有更好的方法或公认的习惯用法?

There is no getting away from assigning one element of the array at a time. 一次分配数组的一个元素并没有困难。 The best you can do is create a function to help with it. 您能做的最好的就是创建一个函数来帮助它。

For example, you could use: 例如,您可以使用:

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
}

and then, use 然后使用

vector_copy(d, i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM