STL example of a class data type

zhaozj2021-02-16  103

Our common data types in STL program is Char Int String, etc. To use complex data types (class types), you must override the necessary operators. The next example demonstrates this:

#include #include Using Namespace std; // Reserved x == rhs.x && this-> y == rhs.y && this-> z x == rhs .x && this-> y x Sortit (List & mylist) {mylist.sort (); return mylist;} main () {list mylist, sortedlist; cmyclass myclass; myclass.x = 3; myclass.y = 2; Myclass.y .z = 'a'; mylist.push_back (myclass); myclass.x = 2; mylist.push_back (myclass); myclass.z = 'b'; mylist.push_back (Mycla SS); myclass.x = 1; myclass.y = 5; myclass.z = 'c'; mylist.push_back (myclass); list :: item i; for (i = myList.begin (); i ! = mylist.end (); i) {cout << ""; myclass.print (* i);} cout << Endl; sortedList = sortit (mylist); cout << "sorted: << ENDL For (i = sortedlist.begin (); i! = Sortedlist.end (); i) {cout << "; myclass.print (* i);

} Cout << endl; return 0;} output:

3 2 A 2 2 A 2 2 B 1 5 C

Sorted: 1 5 C 2 2 A 2 2 B 3 2 A

转载请注明原文地址:https://www.9cbs.com/read-22796.html

New Post(0)