I've overloaded operator << in my own vector class that will work as follows
int main() {
my_vector<int> vec;
vec << 1 << 2 << 3; // will add elements at end [ 1, 2, 3 ]
display(vec);
}
This works perfectly fine as I want, but I wanted to make it more efficient I did this
10 << vec; // will add element at begin [ 10, 1, 2, 3 ]
But this goes wrong when I do it multiple times like
20 << 10 << vec; // this does [ 20480, 1, 2, 3 ];
that first does operation on 20 << 10, then 20480 << vec
I want to process it like [ 20, 10, 1, 2, 3];
One potential solution is to create a proxy object, or even a full-fledged vector object, that aggregates the elements to be inserted.
The interface would look approximately like:
Note that I recommend using the extraction operator (
>>) to indicate front-insertion, as it reads better to me, but feel free to change back to the insertion operator (<<) if you don't like this.Your implementation would require:
operator>>(const my_vector&, my_vector&)to represent front-inserting one vector into another.vec_front_inserterclass that hasoperator>>(vec_front_inserter&, int)defined, and holds amy_vectorinternally.operator>>(const vec_front_inserter&, my_vector&)to insert the elements at the front ofmy_vector.This feels similar to me to string concatenation.
You can do something like:
But you can't do something like: