为什么没有定义operator + for vector? [英] Why is not defined operator+ for vector?

查看:81
本文介绍了为什么没有定义operator + for vector?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么未定义operator + for vector?


例如,

vector< T> v1,v2,v3;

T a;


v1 = v2 + a;

v1 = v2 + v3; < br $>
-

Alex Vinokur

mailto:al **** @ connect.to
http://mathforum.org/library/view/10978.html


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



推荐答案

Alex Vinokur写道:
Alex Vinokur wrote:
为什么没有定义operator + for vector?


这是什么意思?

例如,
vector< T> v1,v2,v3;
T a;

v1 = v2 + a;


这是否使v1成为v2和a的串联,或者为v2的每个元素添加

a的结果?

v1 = v2 + v3;
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;




这是否将v2的每个元素添加到v3中的相应元素,

或make 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?




" Jeff Schwab" < JE ****** @ comcast.net>在消息新闻中写道:6q ******************** @ comcast.com ...

"Jeff Schwab" <je******@comcast.net> wrote in message news:6q********************@comcast.com...
Alex Vinokur写道:
Alex Vinokur wrote:
为什么没有定义operator + for vector?
它是什么意思?
Why is not defined operator+ for vector?
What would it mean?
例如,
vector< T> v1,v2,v3;
T a;

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

v1=v2+a;



这是否使v1成为v2和a的串联,或者将结果添加到v2的每个元素?



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



连接v2和a。


Concatenation of v2 and a.

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



这是否将v2的每个元素添加到v3中的相应元素,
或使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?



连接v2和v3。

-

Alex Vinokur

mailto:al **** @ connect.to
http://mathforum.org/library/view/10978.html


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


>为什么没有定义operator + for vector?
> Why is not defined operator+ for vector?

例如,
vector< T> v1,v2,v3;
T a;

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

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

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




太多的运算符使代码的可读性降低。如果操作员的意思不是明显在它执行的操作的上下文中,那么最好用命名函数替换它。
。在标准库容器的情况下,这些函数

被命名为push_back和insert,IMO更好地描述了追加

操作而不是operator +。


// v1 = v2 + a变为:

v1 = v2;

v2.push_back(a);


// v1 = v2 + v3变为:

v1 = v2;

v1.insert(v1.end(),v3.begin(),v3.end() );


欢呼,

Marcin



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


这篇关于为什么没有定义operator + for vector?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆