0

The recommendation for appending one vector to another (according to Concatenating two std::vectors) is to use:

vector1.insert( vector1.end(), vector2.begin(), vector2.end() );

Why does vector not have a method along the lines of:

vector1.append(vector2);

The only reason that comes to mind is that it may be unclear exactly what append does.

Dead
  • 232
  • 1
  • 10
  • 2
    Why do you need another way of doing something you can already do? – Kerrek SB Jun 08 '17 at 21:07
  • 2
    There isn't a technical reason why it doesn't exist. It's a question of rather you want a fully featured (some would say bloated) interface or a short and concise (some would say incomplete) interface. It's a matter of preference. – François Andrieux Jun 08 '17 at 21:08
  • 3
    probably because `append` would just be a special case of `insert`. `insert` can do much more than put something at the end of something. – Archie Gertsman Jun 08 '17 at 21:09
  • 1
    Because, basically, STL traffics in **ranges** rather than containers. Containers are a convenient way of managing ranges, but ranges are fundamental. `insert` takes a range, which is far more general than having a function that takes its own container type. If `vector` had `append(const vector&)`, what would the appropriate signature be for `list::append? Should it take a `vector` or a `list`? – Pete Becker Jun 08 '17 at 23:07

1 Answers1

3

That's a pretty good reason. In fact, as an apt example, I think you've got the meaning of "append" wrong. I'd expect it to be equivalent to push_back (which exists), whereas you're looking for something more like "concatenate".

Why isn't there a concatenate function? Well, there could have been. But the standard doesn't like to give you things you don't really need, and (as you've shown) this operation is pretty easy to implement yourself.

Mark B
  • 95,107
  • 10
  • 109
  • 188
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    This same argument could be applied for any method besides `insert`, `erase`, `begin` and `end`. All of `vector` besides these methods is trivial to implement, still `push_back` and `size` are provided, because you don't want to reimplement commonly used, trivial stuff (possibly with different names) in each and every project. – Matteo Italia Jun 08 '17 at 21:18
  • 1
    @MatteoItalia: Suggest it to the committee then. But I don't see how this is answerable beyond what I've posed. – Lightness Races in Orbit Jun 08 '17 at 21:19