Noman Ali
Noman Ali

Reputation: 161

How to insert element after the iterator position

std::list.insert inserts an element before the iterator position. How can I insert an element after an iterator position.

Upvotes: 16

Views: 19947

Answers (2)

TanmayChatterjee
TanmayChatterjee

Reputation: 95

Use the function:

itr = mylist.insert_after(Itr, value)

It will insert the value after the position pointed by itr.

Upvotes: 1

Botz3000
Botz3000

Reputation: 39650

that's what list::insert does. Just increment your iterator before inserting your value:

if (someIterator != someList.end()) {
    someIterator++;
}
someList.insert(someIterator, someValue);

Upvotes: 22

Related Questions