C ++ STL learning notes 4 list two-way linked list container

Source: Internet
Author: User

/*
*
**************************************** ****
* Basic description of list two-way linked list containers:
**************************************** ****
*
* The list two-way linked list container uses a two-way linked list data structure to store element data. This allows you to efficiently search, insert, and delete container elements.
*
* Reversibe container back insertion sequence front Insertion Sequence
* Unlike vector, the time complexity of searching, inserting, and deleting elements in list is O (1)
*
* To use list, you must use a macro statement # include <list>
*
**************************************** **************************************** ******
*
* Create a list object:
* 1. List <int>;
* 2. List <int> A (10); // object a with 10 elements. The default value of each element is 0.
* 3. List <char> A (5, 'k ');
* 4. List <char> B (a); // list <char> C (A. Begin (), A. End ())
*
**************************************** **************************************** ******
*
* Initialization assignment
* Void push_back (const T & value)
*
**************************************** **************************************** ******
*
* Traversal
* Iterator begin () // only iterators can be used for traversal.
* Iterator end ()
* Iterator rbegin (); // reverse Traversal
* Iterator rbegin ();
*
**************************************** **************************************** ******
*
* Common functions
*
* Bool empty ();
*
* Void pop_front (); void pop_back ();
*
* Void push_front (const T &); void push_back (const T &);
* Iterator insert (iterator POs, const T & X); // note that it is before the POs
*
* Iterator erase (iterator POS );
* Iterator erase (iterator first, iterator last );
* Void clear ();
* Void remove (const T & value );
*
* Void swap () // exchange the element by switching the header pointer
* // A migration operation provided inside the list
* Void transfer (); // transfer (iterator position, iterator first, iterator last) is inserted before position of a linked list
* // Elements of the iterator range [first, last) of the B linked list, and erase these elements from the B linked list
* Void splice (iterator POs, list & X); // call the transfer function to merge all elements of a linked list into the current linked list,
* // Clear the linked list object X,
* Void splice (iterator POs, list & X, iterator I); // merges the elements referred to by iteration I in X into the POS of the current linked list, and erase the elements indicated by I in X.
*
* Void merge (List & X); // merge two linked lists. Sort them first. Otherwise, merge does not make much sense.
*
* Void unique (); // you can delete consecutive duplicate elements and retain only one
*
*
*
**************************************** ****
* Author: cumirror
* Email: tongjinooo@163.com
**************************************** ****
*
*/

# Include <list>
# Include <iostream>
# Include <string>
Using namespace STD;

Struct student {
Char * Name;
Int age;
Char * city;
Char * phone;
};

Class studentnew {
Public:
Char name [10];
Int age;
Char City [10];
Char phone [11];
Studentnew (){}
Studentnew (char * a, int B, char * C, char * D ){
Strcpy (name, );
Age = B;
Strcpy (city, C );
Strcpy (phone, d );
}

// Why is the error c2593: 'operator> 'is ambiguous returned when a friend's meta function is defined outside the class?
// Friend bool operator <(studentnew & A, studentnew & B );
// Friend bool operator> (studentnew & A, studentnew & B );
// Friend bool operator = (studentnew & A, studentnew & B );

Friend bool operator <(studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name) <0;
}
Friend bool operator> (studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name)> 0;
}
Friend bool operator = (studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name) = 0;
}

Bool operator () (studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name) =-1;
}
};
/*
Bool operator <(studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name) <0? True: false;
}
Bool operator> (studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name)> 0? True: false;
}
Bool operator = (studentnew & A, studentnew & B ){
Return strcmp (A. Name, B. Name) = 0? True: false;
}
*/

Int main (){
/* List <int>;
A. push_back (4 );
A. push_back (3 );
A. push_back (2 );
A. push_back (8 );
A. push_back (7 );
A. push_back (5 );
A. push_back (6 );
A. push_back (9 );
A. push_back (10 );
A. push_back (1 );

// The sort function implementation method of list is as follows:

List <int> carry;
List <int> counter [64];
Int fill = 0;
While (! A. Empty ()){
Carry. splice (carry. Begin (), A, A. Begin ());
Int I = 0;
While (I <fill &&! Counter [I]. Empty ()){
Counter [I]. Merge (carry );
Carry. Swap (counter [I ++]);
}
Carry. Swap (counter [I]);
If (I = fill) ++ fill;
}
For (INT I = 1; I <fill; ++ I ){
Counter [I]. Merge (counter [I-1]);
A. Swap (counter [fill-1]);
}
For (list <int >:: iterator J = A. Begin (); J! = A. End (); j ++ ){
Cout <* j <Endl;
}
*/
// Use other functions as follows:

Student s [] = {
{"Tong Jin", 23, "Wuhan", "XXX "},
{"Boss", 23, "Wuhan", "XXX "},
{"Dumplings", 23, "Wuhan", "XXX "}
};
List <student> classa;
Classa. push_back (s [0]);
Classa. insert (classa. Begin (), s [1]);
Classa. push_back (s [3]);
Cout <classa. Begin ()-> name <Endl;
// Classa. Sort (); // for user-defined struct, there is no overload </>/= these operators, so they cannot be sorted

// Create a new class studentnew overload operator and then judge
Studentnew M1 ("Tong Jin", 23, "Wuhan", "XXX ");
Studentnew m2 ("boss", 23, "Wuhan", "XXX ");
Studentnew m3 ("dumplings", 23, "Wuhan", "XXX ");
List <studentnew> classnew;
Classnew. push_back (M1 );
Classnew. push_back (m2 );
Classnew. push_back (m3 );
// Determine whether the friend function works
If (M1> m2 ){
Cout <"operations in the new class have been reloaded successfully" <Endl;
}
// When using the sgi stl Library
// Use the function object studentnew to replace greater <t>
Classnew. Sort (studentnew ());
// If you use the STL library that comes with VC, you can write it in this way. Why is there such a difference? I still don't know.
// Classnew. Sort ();
For (list <studentnew>: iterator M = classnew. Begin (); m! = Classnew. End (); m ++) {// The result shows that
Cout <m-> name <Endl; // classnew has been rearranged
}

List <string> classb;
Classb. push_back ("tong ");
Classb. push_back ("Lan ");
Classb. push_front ("Zhang ");
Classb. push_back ("tong ");
Classb. Sort (); // For the string type, there is a default type greater <t>
Classb. Unique (); // remove duplicate data
For (list <string >:: iterator K = classb. Begin (); k! = Classb. End (); k ++ ){
Cout <* k <Endl;
}
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.