C++ operator overloading adding 3 vectors together

我只是一个虾纸丫 提交于 2020-05-17 08:09:37

问题


For now this is how I add 3 vectors of type Product together:

    vector1.insert(std::end(vector1), std::begin(vector2), std::end(vector2));
    vector1.insert(std::end(vector1), std::begin(vector3), std::end(vector3));

How do I use operator overloading (I assume overloading the + and = operators) to simplify my code? Product has the following properties:

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

回答1:


Operating overloading is just a normal free function, or member function.

There's nothing special about them, mostly. (The "mostly" referring to operator precedence and some caveats for things like operator* dereferencing or operator,.)

Here is an example using operator+= and append showing they do the same thing:

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



回答2:


I would not assume overloading operations for standard containers as a good idea. However, I would write a function to concatenate a bunch of vectors. Having a C++11 you can use std::initializer_list to have the argument lists of a variable length with ease.

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

And then call this function like that:

vector1 = concatVectors({vector1, vector2, vector3});

Curly braces initialize a new instance of std::initializer_list and that works like magic for any number of vectors.



来源:https://stackoverflow.com/questions/49601019/c-operator-overloading-adding-3-vectors-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!