List: splice () function details

Source: Internet
Author: User
List: splice implements list concatenation. Delete part or all of the elements in the source list and insert them to the destination list.

The function has the following three 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 first, iterator last );

Function Description: move elements between lists:

Moves the elements of X to the specified position of the target list, efficiently inserts them into the target list, and deletes them from X.

The size of the destination list will increase, and the size will be the size of the inserted element. The size of X decreases accordingly.

The first two functions do not involve element creation or destruction. The third function will.

The iterator pointing to the deleted element becomes invalid.

Parameters:

Position

Position of the destination list, used to indicate the insertion position

X

Source list,

First, last

The iterator of the elements to be moved in X. The range is [first, last ).

It contains the first element and does not contain the last element.

Example:

Code

// Splicing lists

# Include < Iostream >

# Include<List>

# Include<String>

# Include<Algorithm>

Using NamespaceSTD;

IntMain ()

{

List<Int>Mylist1, mylist2;

List<Int>: Iterator it;

//Set some initial values:

For(IntI=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(IntI= 0; I< 3; I++)

Its++;

Dictionary. splice (its, bword );

Copy (bword. Begin (), bword. End (), ostream_iterator<String>(Cout,"\ N"));

 Return 0;

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/bichenggui/archive/2009/10/15/4674900.aspx

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.