C + + STL Standard Template Library (list)

Source: Internet
Author: User

//Use of List#define_crt_secure_no_warnings#include<iostream>#include<list>using namespacestd;/*referencing the header file #include <list> thelist class itself is a class template an inner class of iterators in the list of lists of list class templates This class implements a list element pointer function is a linear list structure, Its data consists of several nodes, each of which includes a block of information (that is, the data that is actually stored), a precursor pointer, and a back-drive pointer. It does not need to allocate the specified memory size and can scale arbitrarily, because it is stored in a non-contiguous memory space and is linked by an ordered element by means of the 5-pin. Because of its structural reasons, the list random retrieval performance is very bad, because it does not directly find the address of the element as a vector, but to find the order from the beginning to the other, so that the more the target element, its retrieval time is longer. The retrieval time is proportional to the position of the target element. Although the speed of random retrieval is not fast enough, it can be quickly inserted and deleted at any node. Because each node of the list holds its position in the linked list, inserting or deleting an element affects only a maximum of three elements, unlike vectors, which have no effect on the storage address of all elements after the operation point. List features: (1) Do not use continuous memory space so that can be arbitrarily dynamic operation, (2) can be quickly inserted or removed from any place inside, of course, can also be push and pop at both ends. (3) No internal random access, that is, [] operator and vector.at () is not supported, lists elements are stored sequentially in the linked list, which allows fast insertion and deletion compared to vectors (vectors), but random access is slower.*/classstudent{ Public:    intAge ; Charname[ -];};voidPrint () {}voidProtecta () {Student S1, S2, S3; S1.age= A; strcpy (S1.name,"Millet"); S2.age= -; strcpy (S2.name,"Little Red"); S3.age= -; strcpy (S3.name,"Xiao Gang"); List<student *>Ldata; //add an element from behindLdata.push_back (&S1); Ldata.push_back (&S2); Ldata.push_back (&S3); //Defining iterators//list<student *>::iterator current = NULL; //Error : Error C2440: "Initialize": Cannot convert from "int" to "std::_list_iterator<std::_list_val<std::_list_simple_types<" Student *>>> "//Description List<student *>::iterator is essentially an inner class of the List<student *> class//this inner class overloads the = operator constructor to accept a pointer        /*The begin () method returns an iterator to the element of the linked header*/List<student *>::iterator current =Ldata.begin (); //Emphasizing that current is an inner class is not a pointer but the function of an iterator is equivalent to a pointer to a list element//iterator step is a byte of element type size     for(current, Current! = Ldata.end (); current++) {Student*temp = *Current ; //iterators This inner class overloads the * operatorcout <<"Student Name:"<< Temp->name <<"The age of the student is:"<< Temp->age <<Endl; }}voidPROTECTB () {Student S1, S2, S3; S1.age= A; strcpy (S1.name,"Millet"); S2.age= -; strcpy (S2.name,"Little Red"); S3.age= -; strcpy (S3.name,"Xiao Gang"); List<Student>Ldata;    Ldata.push_back (S1);    Ldata.push_back (S2);    Ldata.push_back (S3); List<student>::iterator current =Ldata.begin ();  for(current, Current! = Ldata.end (); current++)    {        //Student *temp = current; //Error: 2 IntelliSense: Does not exist from "std::_list_iterator<std::_list_val<std::_list_simple_types<student>> > "To" Student * "the appropriate conversion function G:\test\SolutionC3\C001\tec01.cpp//simply not implemented = operator overloading so cannot be assigned//2 operand the left operand type is Student * user did not write C + + compilation cannot be compiled bycout <<"Student Name:"<< Current->name <<"The age of the student is:"<< Current->age <<Endl; //iterator inner class overloaded with operator    }}voidMain () {protecta (); System ("Pause");}

C + + STL Standard Template Library (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.