开发者

why no += operator for vectors in stl

I am curious? What high fundu logic goes behind not implementing:

result+=vector1;

where both result and vector1 are stl ve开发者_高级运维ctors.

Note: i know how to implement that bit, but i need to know what logic, the sages who designed STL were using when they chose not to implement this feature?


What are you expecting result to contain, the result of concatenating the original result with vector1 or an element-wise += (whatever that means for the underlying types), possibly default-initializing members if the sizes don't match?

And yes, this is an answer ;) .

Operator overloading should only be used where the meaning of the operation is unambiguous and practical for the underlying type and where it would offer a significant notational brevity over appropriately named function calls.

Note that valarray has more operator overloads than vector due to its interface and intended use.


I think the operator is not overloaded because its meaning is ambiguous. Do you mean to append the vectors? result.insert( result.end(), vector1.begin(), vector1.end() ) accomplishes that.


My take on things (based, in large part on Elements of Programming) is that Stepanov considers containers as substantially less important than algorithms and iterators. From his viewpoint, the algorithms are the most important part. Iterators are secondary, and containers are simply the storage necessary for iterators to do their job.

In the book, he starts off with some basics like objects and transformations. Chapter six is devoted primarily to iterators (in fact, "Iterators" is the name of the chapter). That also has nearly the only mention of containers in the book:

There are four kinds of linear traversal: single-pass forward (an input stream), multipass forward (a singly linked list), bidirectional (a doubly linked list), and random access (an array).

The only other place I remember him mentioning containers at all is to point out that a particular type (a "linearizable") isn't a container because it doesn't own its contents.

There are also some other arguments to be made, such as minimizing duplication and avoiding ambiguity, but personally I think they're secondary (at best). The STL attempts to generalize algorithms to the greatest possible degree. Containers are de-emphasized, and a large part of the point of iterators is (or at least to me seems to be) to ensure that they stay in the background.


The Boost.Assignment library does, in fact, add a += operator to std::vector and other containers.

http://www.boost.org/doc/libs/1_42_0/libs/assign/doc/index.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜