Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::vector::insert, is it ok to call it with end as first parameter?

Tags:

c++

stl

As for the title, is it ok to pass vec.end() as the position parameter? I.e., is the behaviour of

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

well defined?

like image 704
Stefano Falasca Avatar asked Dec 12 '22 11:12

Stefano Falasca


1 Answers

Yes, it is well defined. Assume if vector is empty, begin() equals to end(). The effects is it inserts a copy of element before iterator.

§ Table 100 — Sequence container requirements (in addition to 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 look at: std::vector::insert

like image 181
billz Avatar answered Dec 14 '22 01:12

billz