Aswin Prasad
Aswin Prasad

Reputation: 417

Difference between splice and insert

Recently I came across functions of list container in STL, i saw

list::splice and list::insert

For me it seems that both does the same job of inserting, I can't specifically find a difference, also i can't find the difference anywhere. So what is specifically the difference, and is there anything special to splice?

Upvotes: 3

Views: 1688

Answers (2)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

splice will concatenate two lists in O(1) time, without any copying of data. insert involves copying data, and if you want to concatenate two lists using insert, it will take O(N) time, where N is the number of elements in the list you want to append.

Also, splice will modify (actually, empty), the appended list.

Upvotes: 3

nate
nate

Reputation: 1871

Splice will move items from the source to the destination. Insert will copy them from the source to the destination. Splice is faster, but will modify the source list, whereas insert takes longer but leaves the original list intact.

Upvotes: 3

Related Questions