#include <iostream> #include<algorithm> #include <string> #include <list> using namespace std; //3.6.4.1 list constructor //List < T > lstt; / / list is implemented by using template class. The default construction form of the object is as follows: //list(beg, end); / / the constructor copies the elements in the [beg, end) interval to itself. //list(n, elem); / / the constructor copies n elems to itself. //List (const list & LST); / / copy the constructor. //3.6.4.2 insert and delete list data elements //Push back (elem); / / add an element at the end of the container //pop_back(); / / delete the last element in the container //Push "front (elem); / / insert an element at the beginning of the container //pop_front(); / / remove the first element from the beginning of the container //insert(pos, elem); / / insert a copy of the elem element at the pos location to return the location of the new data. //insert(pos, n, elem); / / insert n elem data at pos, no return value. //Insert (pos, beg,end); / / insert the data in the [beg,end) interval at the pos position, no return value. //clear(); / / remove all data from the container //Erase (beg,end); / / delete the data in the [beg,end) range and return the location of the next data. //erase(pos); / / delete the pos location data and return to the next data location. //remove(elem); / / delete all elements in the container that match the elem value. // // //3.6.4.3 list size operation //size(); / / returns the number of elements in the container //empty(); / / judge whether the container is empty //resize(num); / / specify the length of the container as num, //If the container becomes longer, the new location is populated with the default value. //If the container becomes shorter, elements that end longer than the length of the container are removed. //resize(num, elem); / / specify the container length as num, //If the container is longer, fill the new location with an elem value. //If the container becomes shorter, elements that end longer than the length of the container are removed. // //3.6.4.4 list assignment //assign(beg, end); / / assign the data copy in the [beg, end) interval to itself. //assign(n, elem); / / assign N copies of elem to itself. //List & operator = (const list & LST); / / overload the equal sign operator //swap(lst); / / swap lst with its own elements. //3.6.4.5 access to list data //front(); / / returns the first element. //back(); / / returns the last element. //3.6.4.6 list reverse sorting //reverse(); / / reverses the linked list. For example, lst contains 1,3,5 elements. After running this method, lst contains 5,3,1 elements. //sort(); //list sort //print data void printInt(list<int>& l) { for (list<int>::iterator it = l.begin(); it != l.end(); ++it) { cout << *it << endl; } } void test1() { list<int> l; for (int i = 0; i < 5; i++) { l.push_back(i); } list<int> l2(10, 88); list<int> l3(l2.begin(), l2.end()); printInt(l2); printInt(l3); l2.push_back(100); //Reverse print for (list<int>::reverse_iterator it = l2.rbegin(); it != l2.rend(); ++it) { cout << *it << endl; } // list container does not support random access iterators list<int>::iterator it_1 = l.begin(); //it_1 = it_1 + 1; // Insert data tail plug list<int> l1; l1.push_back(23); l1.push_front(233); //Delete data at both ends l1.pop_back(); l1.pop_front(); // Insert insert insert data l1.insert(l1.begin(), 1000); //remove delete data l1.remove(1000); // list size cout << l1.size() << endl; } bool intPare(int a, int b) { return a > b; } void test2() { list<int> l4; l4.assign(5, 2); //printInt(l4); list<int> l5; l5.push_back(234); l5.push_back(89); l5.assign(l4.begin(), l4.end()); // So there's no data before l5 printInt(l5); l5.push_front(563); // Containers that do not support random access to iterators cannot use system sort // X sort(l5.begin(), l5.end()); l5.sort();// Default ascending order printInt(l5); l5.sort(intPare); // Custom sort printInt(l5); l5.reverse();// Reversal } //list operation custom type class Person { public: Person(string name, int age, int height) { this->m_name = name; this->m_age = age; this->m_height = height; } bool operator== (const Person &p) { return this->m_age == p.m_age && this->m_height == p.m_height && this->m_name == p.m_name; } string m_name; int m_age; int m_height; }; // In descending order of age, in ascending order of height bool personParse(Person &p1, Person &p2) { if (p1.m_age == p2.m_age) { return p1.m_height < p2.m_height; } return p1.m_age > p2.m_age; } void printPerson(const list<Person> &l) { for (list<Person>::const_iterator it = l.begin(); it != l.end(); ++it) { cout << (*it).m_name << "age is" << (*it).m_age << "height is" << (*it).m_height << endl; } } void test3() { list<Person> l; Person p1("A", 50, 172); Person p2("B", 23, 182); Person p3("C", 78, 178); Person p4("D", 29, 183); Person p5("E", 29, 180); l.push_back(p1); l.push_back(p2); l.push_back(p3); l.push_back(p4); l.push_back(p5); //l.sort(); for custom data types, you need to customize the collation l.sort(personParse); printPerson(l); // list container delete custom type data Person p6("E", 29, 180); l.remove(p6); //??? I don't know why I made a mistake here, // Binary '= =': no operator (or no acceptable conversion) found to accept a left operand of type 'const'ty' printPerson(l); } int main() { //test1(); //test2(); test3(); return 0; }
List is a kind of non continuous and non sequential storage structure on the physical storage unit. The logical order of data elements is realized through the pointer link order in the linked list.
The ist container is not only a two-way list, but also a circular two-way list.
list supports bidirectional iterators
Sort sorting of the system is not supported. list has its own sort member function
doubt:??? list is reporting an error when deleting a custom data type
Binary '= =': no operator (or no acceptable conversion) found to accept left operands of type 'const'ty'
Here, first record it, later understand it, and then solve it.