List::splice () function of __ function

Source: Internet
Author: User

List::splice to achieve the function of list stitching. Delete the contents of the source list, or all of the elements, into the destination list.

The function has the following three kinds of declarations:

void Splice (iterator position, list<t,allocator>& x); //

void Splice (iterator position, list<t,allocator>& X, iterator i);

void Splice (iterator position, list<t,allocator>& x, iterator-I, iterator last);

Function Description: Move elements between list:

Move the elements of X to the specified location in the destination list and efficiently insert them into the destination list and remove them from X.

The size of the destination list increases and the size of the inserted element is increased. The size of X will decrease the same size accordingly.

The first two functions do not involve the creation or destruction of elements. A third function will.

The iterator that points to the deleted element is invalidated.

Parameters:

Position

The position of the destination list, used to indicate the insertion position.

X

Source list,

First,last

An iterator for the elements in X that need to be moved. The interval is [a].

Contains the element that is pointed to, and does not contain the element that last points to.

Example:

Splicing lists

#include <iostream>

#include <list>

#include <string>

#include <algorithm>

using namespace Std;

int main ()

{

List<int> Mylist1, Mylist2;

List<int>::iterator it;

Set some initial values:

for (int i=1; i<=4; i++)

Mylist1.push_back (i); Mylist1:1 2 3 4

for (int i=1; i<=3; i++)

Mylist2.push_back (I*10); Mylist2:10 20 30

it = Mylist1.begin ();

++it; Points to 2

Mylist1.splice (it, mylist2); Mylist1:1 10 20 30 2 3 4

Mylist2 (empty)

"It" still points to 2 (the 5th Element)

Mylist2.splice (Mylist2.begin (), mylist1, it);

Mylist1:1 10 20 30 3 4

Mylist2:2

"It" is now invalid.

it = Mylist1.begin ();

Advance (it,3); "It" points now to 30

Mylist1.splice (Mylist1.begin (), Mylist1, it, Mylist1.end ());

MYLIST1:30 3 4 1 10 20

cout << "mylist1 contains:";

For (It=mylist1.begin (); It!=mylist1.end (); it++)

cout << "" << *it;

cout << "/nmylist2 contains:";

For (It=mylist2.begin (); It!=mylist2.end (); it++)

cout << "" << *it;

cout << Endl;

list<string> dictionary, Bword;

Dictionary.push_back ("any");

Dictionary.push_back ("angle");

Dictionary.push_back ("Ajust");

Dictionary.push_back ("common");

Dictionary.push_back ("Cannon");

Dictionary.push_back ("Company");

Bword.push_back ("Blue");

Bword.push_back ("banana");

Bword.push_back ("break");

List<string>::iterator its = Dictionary.begin ();

for (int i = 0; i < 3; i++)

its++;

Dictionary.splice (its, bword);

Copy (Bword.begin (), Bword.end (), ostream_iterator<string> (cout, "/n"));

return 0;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.