tags:

views:

216

answers:

4

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

result+=vector1;

where both result and vector1 are stl vectors.

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?

+3  A: 

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.

Asher Dunn
yeouch. that isn't the only way, is it? `vec1.append(vec2)` would be pretty unambiguous, but much more friendly.
Mark
For your toolbox: `template<typename C1, typename C2> void append(C1 }`. Not every possible function is in the STL, and quite a few trivial functions are missing in favor of more complex functions.
MSalters
+15  A: 

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.

Charles Bailey
Thanks for the answer.
This can be a gotcha when using Python and Numpy, for example. In python, the + operator concatenates lists, but it is an element-wise addition in numpy arrays.
kibibu
I wish more languages took the approach of making addition and concatenation separate operators.
dan04
+2  A: 

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.

Jerry Coffin
I dont get it! My understanding was that Abstractions are more important in C++ than implementations, then why does STL focuses so much on implementations, i.e. different types of algorithms, rather than abstractions such as containers. I can live without a count_if function, but a keyset function for STL Maps is something of more help.
One of the stark difference between C++ and Python/JAVA is that data structures such as lists, and hashmaps are extremely easier to handle in later.
A: 

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

Nate