0

Why was + not defined as a concatenation operator in C++ vectors? For example, + is allowed on strings, + is also allowed on python lists. Wouldn't it make things convenient?

Ekalavya
  • 969
  • 1
  • 9
  • 14
  • Why should it be convinient to add vectors in c++, only because you can add python lists? – RoQuOTriX Sep 17 '19 at 11:19
  • 1
    You can always implement it yourself :) The standard is there to provide you with generic tools. – Mansoor Sep 17 '19 at 11:23
  • 5
    I've written hundreds (or thousands?) of string concatenations in my C++ career. I believe I could count the `std::vector` concatenations on my fingers. – Angew is no longer proud of SO Sep 17 '19 at 11:28
  • 4
    Because `operator+` can have 2 meaning for vectors: element wise addition or concatenation. Whichever is picked will be "wrong" and you can always implement the one you need yourself. – Richard Critten Sep 17 '19 at 11:50
  • @Angew: LSTM's are rather popular in neural networks at the moment. Theoretically they use 12 tensors per step, but a smart C++ implementation concatenates 12 vectors for locality of reference. So I now have 11 vectors concatenations ;) – MSalters Sep 17 '19 at 13:07

1 Answers1

-6

Because, unlike strings, vectors are not naturally +'ed only by concatenating them; in many (most?) cases, vectors are +'ed by +'ing their elements element-wise.

L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
  • 1
    what you describe is `std::valarray`. For `std::vector` it is, forgive me, rather far-fetched claims – 463035818_is_not_an_ai Sep 17 '19 at 11:38
  • If you say so. You may want to check the accepted answer on the "this is a duplicate of" link. – L. Scott Johnson Sep 17 '19 at 11:49
  • 2
    Strictly speaking, down-voting is for answers one finds "note useful". Says as much in the popup when you hover over the downvote button. An answer can be not useful for a variety of reasons, not just if it's "bad" (which is also a fairly subjective term). – StoryTeller - Unslander Monica Sep 17 '19 at 11:56
  • 1
    I downvoted as I don't find this answer useful. I've also downvoted the accepted answer on the duplicate. Because that one is not useful either. (I conject the standard was lower on Stack Overflow in 2011 than it is now.) – Bathsheba Sep 17 '19 at 13:24
  • OK, well thanks for the info. I'm not sure why the answers aren't useful, however -- they do answer the question as to why the + operator wasn't implemented in the STL for vectors. – L. Scott Johnson Sep 17 '19 at 13:26