Std :: vector :: insert, is it normal to call it as the first parameter?

As for the name, is it possible to pass vec.end() as a position parameter? Ie, is the behavior

 std::vector<int> vec; vec.insert(vec.end(), 0); 

clearly defined?

+4
source share
2 answers

Yes, it is clearly defined. Suppose that if the vector is empty, begin() is equal to end() . Effects This inserts a copy of the element before the iterator.

Β§ Table 100 - Sequence container requirements (in addition to the container)

 |------------------------------------------------------------------------------| |a.insert(p,t) | iterator Requires:T shall be CopyInsertable into X. For | | | vector and deque, T shall also be CopyAssignable.| | | Effects: Inserts a copy of t before p. | -------------------------------------------------------------------------------| 

also see: std :: vector :: insert

+7
source

Yes, the iterator passed in the insert function gives the position before which a new element is inserted. Thus, setting to end() makes the item last in the container.

+6
source

Source: https://habr.com/ru/post/1502049/


All Articles