Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ insert function

C++ insert function

C++ Container Class The C++ container class


insert is a function in vector used toinserts an element at a specified positionfunction; it is the most flexible insertion method in the container.

insert is a member function of the container class, used toInserts one or more elements at a specified position in the container. It accepts an iterator position and the element to insert.

insert Provides the ability to insert elements at any position, although in vector it is less efficient than push_back(which requires moving subsequent elements), but it is more powerful.

Word meaning: insert means "insert," i.e., to add elements at a specified position.


Basic syntax and parameters

insert is a member function of the container class that requires specifying the insertion position and the element.

Syntax format


// Insert a copy of value at pos position
iterator insert(iterator pos, const T& value);

// Insert a copy of value at pos (move)
iterator insert(iterator pos, T&& value);

// Insert n copies of the value at pos position
void insert(iterator pos, size_type n, const T& value);

// Insert elements in the range [first, last) at pos
template
void insert(iterator pos, InputIt first, InputIt last);

// Initialize list insertion method
void insert(iterator pos, std::initializer_list ilist);

Parameter Description

- Type: iterator (`iterator`) - Description: the iterator of the insertion position. The new element will be inserted before `pos`.