读书人

c++ stl list应用总结

发布时间: 2012-11-05 09:35:12 作者: rapoo

c++ stl list使用总结

#pragma warning(disable:4786)#include <iostream>#include <list>#include <cmath>#include <string.h>using namespace std;class Person{    char * name;    char sex;    int age;public:    // constructor    Person()    {        name = new char[strlen("Anonymous") + 1];        sex = 'N';        age = 0;    }    // constructor    Person(char * pName, char pSex, int pAge)        : sex(pSex), age(pAge)    {        name = new char[strlen(pName) + 1];        strcpy(name, pName);    }    // copy constructor    Person(const Person& rhs)        : sex(rhs.sex), age(rhs.age)    {        name = new char[strlen(rhs.name) + 1];        strcpy(name, rhs.name);    }    // overload the assignment operator    Person& operator=(const Person& rhs)    {        name = new char[strlen(rhs.name) + 1];        strcpy(name, rhs.name);        sex = rhs.sex;        age = rhs.age;        return *this;    }    // overload the == operator    // for sorting purposes, we consider that two Person objects are "equal"    // if they have the same age    bool operator==(const Person& rhs) const    {        return (age == rhs.age) ? true : false;    }    // overload the < operator    // for sorting purposes, we consider that a Person object is "less than" another    // if it's age is less than the other object's age    bool operator<(const Person& rhs) const    {        return (age < rhs.age) ? true : false;    }    // overload the > operator    // for sorting purposes, we consider that a Person object is "greater than" another    // if it's age is greater than the other object's age    bool operator>(const Person& rhs) const    {        return (age > rhs.age) ? true : false;    }    // print the object    void print()    {        cout << name << " " << sex << " " << age << endl;    }    // destructor    ~Person()    {        delete []name;    }};void print(list<Person> lst, char * name){    list<Person>::iterator it;    cout << name << ":" << endl;    for(it = lst.begin(); it != lst.end(); ++it)        it->print();    cout << endl;}int main(){list<Person> lst;// create some Person objectsPerson p1("Bill Gates", 'M', 50);Person p2("Scott Meyers", 'M', 43);Person p3("Charles Petzold", 'M', 48);Person p4("Christina Dermayr", 'F', 30);Person p5("Andrei Neagu", 'M', 22);Person p6("Yin Su", 'F', 56);Person p7("Georgeta Bills", 'F', 37);// add the objects to the listlst.push_back(p1);lst.push_back(p2);lst.push_back(p3);lst.push_back(p4);lst.push_back(p5);lst.push_front(p6);lst.push_front(p7);print(lst, "lst");// sort the list in ascending orderlst.sort( less<Person>() );print(lst, "lst in ascending order");// sort the list in descending orderlst.sort( greater<Person>() );print(lst, "lst in descending order");// delete the first element from the listlst.pop_front();print(lst, "lst");// clear the listlst.clear();if(lst.empty())cout << "lst is empty" << endl;elsecout << "lst is not empty" << endl;return 0;// Output// lst:// Georgeta Bills F 37//Yin Su F 56// Bill Gates M 50// Scott Meyers M 43// Charles Petzold M 48// Christina Dermayr F 30// Andrei Neagu M 22//// lst in ascending order:// Andrei Neagu M 22// Christina Dermayr F 30// Georgeta Bills F 37// Scott Meyers M 43// Charles Petzold M 48// Bill Gates M 50// Yin Su F 56// // lst in descending order:// Yin Su F 56// Bill Gates M 50// Charles Petzold M 48// Scott Meyers M 43// Georgeta Bills F 37// Christina Dermayr F 30// Andrei Neagu M 22//// lst:// Bill Gates M 50// Charles Petzold M 48// Scott Meyers M 43// Georgeta Bills F 37// Christina Dermayr F 30// Andrei Neagu M 22//    // lst is empty}



参考:http://www.codeproject.com/Articles/20960/The-complete-guide-to-STL-Part-2-List
转载请声明:http://blog.csdn.net/nuptboyzhb/article/details/8120397

读书人网 >C++

热点推荐