STL container function return values

后端 未结 8 1137
名媛妹妹
名媛妹妹 2021-01-05 03:41

When looking over the member functions of the STL containers, an odd thought occurred to me. Why don\'t functions like std::vector::push_back(T) not ha

相关标签:
8条回答
  • 2021-01-05 04:42

    Because there's .back() that will instantly return it for you?

    Conceptually, the C++ designers won't implement in a member function anything that would be difficult or impossible for you to implement in a public interface. Calling .back() is simple and easy enough. For an iterator, you could do (end - 1) or just auto it = end; it--;

    The Standards committee makes new code possible and massively simplifies code that is very commonly used. Stuff like this just isn't on the list of things to do.

    0 讨论(0)
  • 2021-01-05 04:44
    v.insert(v.end(),x);
    

    Would be equivalent to push_back with returning an iterator. Why push_back itself doesn't return an iterator is beyond me.

    0 讨论(0)
提交回复
热议问题