list双向链表
#include <iostream>
#include <cstring>
#include <list>
using namespace std;
struct Contact
{
string name;
int num;
Contact(const string& _name, const int& _num) {
name = _name;
num = _num;
}
bool operator < (const Contact& itemToCompare) const {
return this->num < itemToCompare.num;
}
};
bool descending(const int& lsh, const int& rsh) {
return lsh > rsh;
}
int main(int argc, char const *argv[])
{
list<int> li = {12, 23};
info(li);
li.push_back(100);
li.push_front(-1);
info(li);
li.insert(li.begin(), 2, 20);
info(li);
li.pop_back();
li.pop_front();
info(li);
li.reverse();
info(li);
li.sort();
info(li);
li.sort(descending);
info(li);
Contact c1("kavin", 12);
Contact c2("jack", 23);
Contact c3("lisa", 34);
list<Contact> contacts = {c1, c2, c3};
contacts.sort();
for (auto it : contacts) {
it.info();
}
return 0;
}
name=kavin, num=12
name=jack, num=23
name=lisa, num=34