How to use C++list

Source: Internet
Author: User

/* First of all, I am a C language enthusiast, has not contacted C + +, and now began to learn C + +, so always and C language contrast, so-called container, is actually linked list! In the C language data structure mentioned, C + + to encapsulate it into a library, do not need to be as specific as the previous learning data structure to implement, as long as the institute, will call on it.

*/

#include "stdafx.h"

#include <iostream>

#include <list>

#include <windows.h>

using namespace Std;

typedef list<int> Intlist; Define a sequence intlist, and the next time you meet him, it can be equivalent to an array or a container or an iterator.


Show all elements of list queue from front to back

void Put_list (intlist list, char *name)

{

Intlist::iterator plist;


cout << "The contents of" << name << ":";

for (plist = List.begin (); Plist! = List.end (); plist++)

cout << *plist << "";

cout << Endl;

}


Testing the functionality of the list container

void Main (void)

{

List1 object is initially empty

Intlist List1;

Intlist List2 (5, 1);

Intlist List3 (List2.begin (),--list2.end ());


Declares a bidirectional iterator named I

Intlist::iterator i;


Put_list (List1, "List1"); Print the lists

Put_list (List2, "List2");

Put_list (List3, "list3");


List1.push_back (7); Mask the Lseven num put out

List1.push_back (8);

cout << "List1.push_back (7) and List1.push_back (8):" << Endl;

Put_list (List1, "List1");


List1.push_front (6); Press the first element 6 into the first element

List1.push_front (5);

cout << "List1.push_front (6) and List1.push_front (5):" << Endl;

Put_list (List1, "List1");


List1.insert (List1.begin (), 3, 9); List1 First straight add 3 9.

cout << "List1.insert (List1.begin () +1,3,9):" << Endl;

Put_list (List1, "List1");


Test Reference class functions

cout << "list1.front () =" << List1.front () << Endl;

cout << "list1.back () =" << list1.back () << Endl;


List1.pop_front ();

List1.pop_back ();

cout << "List1.pop_front () and List1.pop_back ():" << Endl;

Put_list (List1, "List1");


List1.erase (++list1.begin ());//Erase the first element

cout << "List1.erase (++list1.begin ()):" << Endl;

Put_list (List1, "List1");


List2.assign (8, 1);

cout << "List2.assign (8,1):" << Endl;

Put_list (List2, "List2");


cout << "List1.max_size ():" << list1.max_size () << Endl;

cout << "List1.size ():" << list1.size () << Endl;

cout << "List1.empty ():" << list1.empty () << Endl;


Put_list (List1, "List1");

Put_list (List3, "list3");

cout << "List1>list3:" << (List1 > List3) << Endl;

cout << "List1<list3:" << (List1 < LIST3) << Endl;


List1.sort ();

Put_list (List1, "List1");


List1.splice (++list1.begin (), LIST3);

Put_list (List1, "List1");

Put_list (List3, "list3");

Sleep (10000);

}



How to use C++list

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.