标签:push col rcp class begin find 查找 pre uac
list底层为链表,非连续内存,不支持[]操作符,支持任意位置的插入操作。
容量:
a.元素个数:list.size()
b.判断是否为空:list.empty()
修改:
a.尾部添加元素:list.push_buack()
b.首部添加元素:list.push_front()
c.删除尾部元素:list.pop_back()
d.删除首部元素:list.pop_front()
e.插入1:list.insert(pos, num) 在pos位置插入元素num
f.插入2:list.insert(pos, n, num) 在pos位置插入n个元素num
g.插入3: list.insert(pos, beg, end) 在pos位置插入另外一块起始位beg到end的元素
迭代器:
a.list.begin() 指向链表第一个元素的迭代器
b.list.end() 指向链表最后一个元素之后的迭代器
访问:
a.访问第一个元素:list.front()
b.访问最后一个元素:list.back()
删除:
a.清空:list.clear()
b.删除指定元素:list.erase(it)
1 //简单存储 2 list<int> iList; 3 iList.push_back(2); //尾部添加 4 iList.push_back(3); 5 iList.push_front(1); //首部添加 6 7 //存储结构体 8 list<Student> stuList; 9 Student stu1; 10 strcpy(stu1.name, "woniu201"); 11 stu1.age = 30; 12 Student stu2; 13 strcpy(stu2.name, "beijing"); 14 stu2.age = 30; 15 stuList.push_back(stu1); 16 stuList.push_back(stu2);
1 for (list<Student>::iterator it = stuList.begin(); it != stuList.end(); it++) 2 { 3 cout << "name:" << it->name << endl; 4 cout << "age: " << it->age << endl; 5 }
1 //升序排序1 2 iList.sort(less<int>()); 3 for (list<int>::iterator it = iList.begin(); it != iList.end(); it++) 4 { 5 cout << *it << endl; 6 } 7 8 //升序排序2 9 iList.sort(compare1); 10 for (list<int>::iterator it = iList.begin(); it != iList.end(); it++) 11 { 12 cout << *it << endl; 13 } 14 15 //降序排序1 16 iList.sort(greater<int>()); 17 for (list<int>::iterator it = iList.begin(); it != iList.end(); it++) 18 { 19 cout << *it << endl; 20 } 21 22 //降序排序2 23 iList.sort(compare2); 24 for (list<int>::iterator it = iList.begin(); it != iList.end(); it++) 25 { 26 cout << *it << endl; 27 }
1 list<int>::iterator it = find(iList.begin(), iList.end(), 2); 2 if (it == iList.end()) 3 { 4 cout << "not found" << endl; 5 } 6 else 7 { 8 cout << "found" << endl; 9 }
1 for (list<int>::iterator it = iList.begin(); it != iList.end(); it++) 2 { 3 if (*it == 2) 4 { 5 it = iList.erase(it); 6 it--; 7 } 8 } 9 for (list<int>::iterator it = iList.begin(); it != iList.end(); it++) 10 { 11 cout << *it << endl; 12 }
标签:push col rcp class begin find 查找 pre uac
原文地址:https://www.cnblogs.com/woniu201/p/9846450.html