473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Why is not defined operator+ for vector?

Why is not defined operator+ for vector?

For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;
v1=v2+v3;
--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html



Jul 22 '05 #1
10 2652
Alex Vinokur wrote:
Why is not defined operator+ for vector?
What would it mean?
For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;
Does this make v1 the concatenation of v2 and a, or the result of adding
a to each element of v2?
v1=v2+v3;


Does this add each element of v2 to the corresponding element from v3,
or make v1 the concatenation of v2 and v3?
Jul 22 '05 #2

"Jeff Schwab" <je******@comcast.net> wrote in message news:6q********************@comcast.com...
Alex Vinokur wrote:
Why is not defined operator+ for vector?
What would it mean?
For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;


Does this make v1 the concatenation of v2 and a, or the result of adding
a to each element of v2?

Concatenation of v2 and a.
v1=v2+v3;


Does this add each element of v2 to the corresponding element from v3,
or make v1 the concatenation of v2 and v3?

Concatenation of v2 and v3.
--
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html

Jul 22 '05 #3
> Why is not defined operator+ for vector?

For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;
v1=v2+v3;


Too many operators make code less readable. If operator's meaning is not
obvious in context of the operation it performs, it's better to replace it
with named function. In case of standard library containers these functions
were named push_back and insert, which IMO better describes append
operations than operator +.

// v1 = v2 + a becomes:
v1 = v2;
v2.push_back(a);

// v1 = v2 + v3 becomes:
v1 = v2;
v1.insert(v1.end(), v3.begin(), v3.end());

cheers,
Marcin

Jul 22 '05 #4
Alex Vinokur wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message news:6q********************@comcast.com...
Alex Vinokur wrote:
Why is not defined operator+ for vector? ....v1=v2+a;
Concatenation of v2 and a.

....
v1=v2+v3;


Concatenation of v2 and v3.


This behavior differs from the traditional, mathematical meaning of
adding two vectors. Neither concatenation, nor the mathematical meaning
are "wrong." Since there is no single, obvious meaning of operator+ for
vectors, why should any one definition be part of the standard library?
It can be overloaded easily in client code.
Jul 22 '05 #5
Alex Vinokur 2004-05-19 :
"Jeff Schwab" <je******@comcast.net> wrote in message news:6q********************@comcast.com...
Alex Vinokur wrote:
Why is not defined operator+ for vector?


What would it mean?
For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;


Does this make v1 the concatenation of v2 and a, or the result of adding
a to each element of v2?

Concatenation of v2 and a.


I would have said it adds a to every element of v2
v1=v2+v3;


Does this add each element of v2 to the corresponding element from v3,
or make v1 the concatenation of v2 and v3?

Concatenation of v2 and v3.


I would have said it adds v2 and v3 element by element.

So you see a good reason (among others) why that operator is
*not* defined...
ciao
Walter
Jul 22 '05 #6
Jeff Schwab wrote:

Alex Vinokur wrote:
Why is not defined operator+ for vector?
What would it mean?


This is a weak argument. There are plenty of methods/operators that are far
from intuitive.

Take vector::empty(), for example.

Without prior (stl) knowledge, there is no way to know what this means: does it
test for an empty condition or empty the contents of the vector?
For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;


Does this make v1 the concatenation of v2 and a, or the result of adding
a to each element of v2?
v1=v2+v3;


Does this add each element of v2 to the corresponding element from v3,
or make v1 the concatenation of v2 and v3?

Jul 22 '05 #7
Julie wrote:
Jeff Schwab wrote:
Alex Vinokur wrote:
Why is not defined operator+ for vector?
What would it mean?

This is a weak argument.


It's not an argument. It's a question.
There are plenty of methods/operators that are far
from intuitive.
And more would help?
Take vector::empty(), for example.

Without prior (stl) knowledge, there is no way to know what this means: does it
test for an empty condition or empty the contents of the vector?


You have a good point; a name like "is_empty" might have been better.
Jul 22 '05 #8
Julie wrote:

Jeff Schwab wrote:

Alex Vinokur wrote:
Why is not defined operator+ for vector?
What would it mean?


Preamble: my previous response wasn't meant to be attacking or argumentative.
I don't disagree that specifically _operator_+_ is ambiguous and not warranted
for various reasons pointed out by other respondents.
This is a weak argument. There are plenty of methods/operators that are far
from intuitive.

Take vector::empty(), for example.

Without prior (stl) knowledge, there is no way to know what this means: does it
test for an empty condition or empty the contents of the vector?
For instance,
vector<T> v1, v2, v3;
T a;

v1=v2+a;


Does this make v1 the concatenation of v2 and a, or the result of adding
a to each element of v2?
v1=v2+v3;


Does this add each element of v2 to the corresponding element from v3,
or make v1 the concatenation of v2 and v3?

Jul 22 '05 #9
Jeff Schwab wrote:

Julie wrote:
Jeff Schwab wrote:
Alex Vinokur wrote:

Why is not defined operator+ for vector?

What would it mean?

This is a weak argument.


It's not an argument. It's a question.


Yep, you are absolutely correct. I wasn't thinking clearly at all when I
originally responded -- you can safely ignore it.
There are plenty of methods/operators that are far
from intuitive.
And more would help?


ibid.
Take vector::empty(), for example.

Without prior (stl) knowledge, there is no way to know what this means: does it
test for an empty condition or empty the contents of the vector?


You have a good point; a name like "is_empty" might have been better.

Jul 22 '05 #10
Take vector::empty(), for example.

Without prior (stl) knowledge, there is no way to know what this means: does it
test for an empty condition or empty the contents of the vector?


Look at the full declaration

bool empty() const;

For sure no modifications, returns bool, what's not obvious about it??

JLR
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PengYu.UT | last post by:
I'm wonder whether 1. stl directly defined the 6 comparison operators(== != < > <= >=) directly for iterator and const_iterator 2. or it only define == and < and using std::rel_ops to get the...
3
by: bsa | last post by:
Hi, This maybe a stupid question for which I apologies, but I couldn't find a solutions so I'll ask. I'm a newbie and I'm trying to do the following: A have a base class called space that...
1
by: avimitrani | last post by:
Hi, I am trying to apply the sort() algorithm on a vector of structs. I want the vector to be sorted according to one of the struct fields. I see two ways in which this could be done, and neither...
1
by: Raghuram N K | last post by:
Hi, Following program compiles and executes successfully in windows with DevCPP compiler. When I compile the same in Linux with 'g++323' compiler I get following assignment error: cannot...
3
by: mweltin | last post by:
I have been looking in the archives and as of yet have not found an answer to my problem. Class B has four members, and class A is derived from class B. Class A only adds one new member to class...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
6
by: Pallav singh | last post by:
Hi when we should have Class defined Inside a Class ? can any one give me explanation for it ? Does it is used to Hide some information of Class Data-Member and Function from friend class? ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.