Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;运算符重载将3个向量相加_C++_Oop_Overloading_Operator Keyword - Fatal编程技术网

C++ C++;运算符重载将3个向量相加

C++ C++;运算符重载将3个向量相加,c++,oop,overloading,operator-keyword,C++,Oop,Overloading,Operator Keyword,现在,我将3个乘积类型的向量相加: vector1.insert(std::end(vector1), std::begin(vector2), std::end(vector2)); vector1.insert(std::end(vector1), std::begin(vector3), std::end(vector3)); 如何使用运算符重载(我假设重载+和=运算符)来简化代码?产品具有以下特性: private: std::string url; d

现在,我将3个乘积类型的向量相加:

    vector1.insert(std::end(vector1), std::begin(vector2), std::end(vector2));
    vector1.insert(std::end(vector1), std::begin(vector3), std::end(vector3));
如何使用运算符重载(我假设重载+和=运算符)来简化代码?产品具有以下特性:

private:
    std::string url;
    double cost;
    std::string name;
    std::string site;

操作重载只是一个普通的自由函数或成员函数

大多数情况下,他们没有什么特别之处。(主要是指运算符优先级和一些注意事项,如
运算符*
取消引用或
运算符,

下面是一个使用
operator+=
append
的示例,显示它们执行相同的操作:

#include <iostream>
#include <vector>

using std::begin;
using std::cout;
using std::end;
using std::endl;
using std::ostream;
using std::vector;

struct Product
{
  static int count;
  int i;
  Product() : i{++count} {}
};

static ostream& operator<<(ostream& o, Product const& p)
{
  o << p.i;
  return o;
}

int Product::count = 100;

static void append(vector<Product>& v, vector<Product> const& v2)
{
  v.insert(end(v), begin(v2), end(v2));
}

static vector<Product>& operator+=(vector<Product>& v, vector<Product> const& v2)
{
  v.insert(end(v), begin(v2), end(v2));
  return v;
}

int main()
{
  auto product1 = vector<Product>{};
  product1.push_back(Product{});
  product1.push_back(Product{});
  product1.push_back(Product{});
  product1.push_back(Product{});

  auto product2 = vector<Product>{};
  product2.push_back(Product{});
  product2.push_back(Product{});
  product2.push_back(Product{});
  product2.push_back(Product{});

  auto product3 = vector<Product>{};
  product3.push_back(Product{});
  product3.push_back(Product{});
  product3.push_back(Product{});
  product3.push_back(Product{});

  append(product1, product2);
  product1 += product3;

  char const* sep = "";
  for (auto const& p : product1)
  {
    cout << sep << p;
    sep = " ";
  }
  cout << endl;
}
#包括
#包括
使用std::begin;
使用std::cout;
使用std::end;
使用std::endl;
使用std::ostream;
使用std::vector;
结构产品
{
静态整数计数;
int i;
Product():i{++count}{}
};

静态ostream&operator我不认为标准容器的重载操作是个好主意。但是,我会编写一个函数来连接一组向量。有了C++11,您可以使用
std::initializer\u list
轻松地获得可变长度的参数列表

// your includes ...
#include <initializer_list>

// some code ...

std::vector<Product> concatVectors(std::initializer_list<std::vector<Product>> args)
{
    std::vector<Product> res;
    for (auto v: args) {
        res.insert(std::end(res), std::begin(v), std::end(v));
    }
    return res;
}

花括号初始化
std::initializer\u list
的一个新实例,这对任意数量的向量都很有效。

您认为为什么要使用运算符重载来完成此任务?你做过什么尝试吗?我知道如何使用运算符重载将两种产品的成本相加,但我不确定如何将向量相加。你的目标是什么<代码>向量1+=向量2+向量3/COD>或其他什么?C++运算符是(主要)二元运算符。你的手术是三元的;它有3个操作数。运算符重载在这里是完全错误的工具。只需编写一个函数
merge\u vectors\u in place(vector1、vector2、vector3)
或类似函数。我所说的坏主意是一个类似于
vector\u append(vector1,vector2)的结果;向量_追加(向量1,向量2)
vector1+=vector2;向量1+=向量3。请注意,为向量定义
+=
是出乎意料的。这需要讨论名称空间和ADL,可能还需要讨论std没有重载这些运算符的原因,以及最终用户无法正确执行的原因。不是downvoter,而是ils是常量并包含值,因此强制所有数据的2个副本(输入il和输出)。一个写得好的函数最多只能复制一次(加上一些用于扩展性调整大小的函数)。
vector1 = concatVectors({vector1, vector2, vector3});