【问题标题】:How to Add two multi-dimensional std::vectors (without using operator+)?如何添加两个多维 std::vectors(不使用 operator+)?
【发布时间】:2017-10-15 21:16:34
【问题描述】:

算法简单直接:
继续将n 维向量分解为n-1 维构成向量,直到您可以访问基本数据类型对象。它们的添加简单且定义明确。

这是我使用模板和operator+的实现:

#include <iostream>
#include <vector>

template<class T>
std::vector<T> operator+ (const std::vector<T>& v1, const std::vector<T>& v2)
{
    std::vector<T> output;
    unsigned len = v1.size();
    output.resize(len);
    for (int i = 0; i < len; ++i)
        output[i] = v1[i] + v2[i];
    return output;
}

int main ()
{
    using namespace std;

    vector<std::vector<int>> x = {{8,9,0},{5,6,7}};
    vector<std::vector<int>> y = {{1,1,1},{1,1,1}};

    auto result = x + y; // Yeah, just like that !

    // Being Lazy, instead of implementing multi-dimensional vector printing...
    for (int i = 0; i < result.size(); ++i)
    {
        for (int j=0; j<result[i].size(); ++j)
            cout << result.at(i).at(j) << " ";
        cout << "\n";
    }

    return 0;
}

但是假设你不允许使用operator+。
您必须将界面设计为某些功能Add()。

但是我做不到!!这是我的尝试:

#include <iostream>
#include <vector>

// Hoping to handle vectors...
template<class T>
std::vector<T> Add (const std::vector<T>& v1, const std::vector<T>& v2)
{
    std::vector<T> output;
    unsigned len = v1.size();
    output.resize(len);
    for (int i = 0; i < len; ++i)
        output[i] = Add(v1[i], v2[i]);
    return output;
}

// Hoping to handle basic-datatypes...
template<class T>
T Add (const T& v1, const T& v2)
{
    T output;
    unsigned len = v1.size();
    output.resize(len);
    for (int i = 0; i < len; ++i)
        output[i] = v1[i] + v2[i];
    return output;
}

int main ()
{
    using namespace std;

    vector<std::vector<int>> x = {{8,9,0},{5,6,7}};
    vector<std::vector<int>> y = {{1,1,1},{1,1,1}};

    auto result = Add(x, y); // I wish ! But not happening !

    // Being Lazy, instead of implementing multi-dimensional vector printing...
    for (int i = 0; i < result.size(); ++i)
    {
        for (int j=0; j<result[i].size(); ++j)
            cout << result.at(i).at(j) << " ";
        cout << "\n";
    }

    return 0;
}

有可能吗?

【问题讨论】:

  • 这就是为什么您使用单维数组/向量并使用数学假装它具有多个维度的原因。这样你就可以拥有一个 n 维向量,但只使用一个 for 循环。

标签: c++ multidimensional-array stdvector


【解决方案1】:

您必须为Add() 提供适用于基本数据类型的版本

// Handle basic-datatypes
template<class T>
T Add(const T& x1, const T& x2)
{
  return x1 + x2;
}

// Specialisation for vectors
template<class T>
std::vector<T> Add(std::vector<T> const& v1, std::vector<T> const& v2)
{
  assert(v1.size()==v2.size());
  std::vector<T> result;
  result.reserve(v1.size());
  for(size_t i=0; i!=v1.size(); ++i)
    result.emplace_back(Add(v1[i],v2[i])); // possibly recursive
  return result;
}

【讨论】:

    【解决方案2】:

    而不是这个:

    // Hoping to handle basic-datatypes...
    template<class T>
    T Add (const T& v1, const T& v2)
    {
        T output;
        unsigned len = v1.size();
        output.resize(len);
        for (int i = 0; i < len; ++i)
            output[i] = v1[i] + v2[i];
        return output;
    }
    

    必须是:

    // Hoping to handle basic-datatypes...
    template<class T>
    T Add (const T& v1, const T& v2)
    {
        return v1 + v2;
    }
    

    同样处理basic-datatypes的那个必须在处理vectors的那个之前。

    【讨论】:

    • 虽然 OP 在这种情况下就是这个人 :)
    猜你喜欢
    • 2010-09-17
    • 2015-04-17
    • 2014-04-10
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多