01 list容器
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int> &l)
{
for (list<int>::const_iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int> l1;
l1.push_back(10);
l1.push_back(30);
l1.push_back(50);
l1.push_back(70);
//遍历容器
printList(l1);
//区间方式构造
list<int> l2(l1.begin(), l1.end());
printList(l2);
//拷贝构造
list<int> l3(l2);
printList(l3);
//n个elem
list<int> l4(10, 10001);
printList(l4);
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:list构造方式同其他几个STL容器,熟练掌握即可
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int> &L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//赋值
void test01()
{
list<int> L1;
L1.push_back(10);
L1.push_back(30);
L1.push_back(40);
L1.push_back(70);
printList(L1);
list<int> L2;
L2 = L1;
printList(L2);
list<int> L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int> L4;
L4.assign(10, 232);
printList(L4);
}
//交换
void test02()
{
list<int> L1;
L1.push_back(10);
L1.push_back(30);
L1.push_back(40);
L1.push_back(70);
list<int> L4;
L4.assign(10, 232);
cout << "交换前" << endl;
printList(L1);
printList(L4);
L1.swap(L4);
cout << "交换后" << endl;
printList(L1);
printList(L4);
}
int main()
{
//test01();
test02();
system("pause");
return 0;
}
//总结:list赋值和交换操作能够灵活运用即可
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int>& L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int> L1;
L1.push_back(10);
L1.push_back(30);
L1.push_back(40);
L1.push_back(70);
printList(L1);
//判断是否为空
if (L1.empty())
{
cout << "L1为空" << endl;
}
else
{
cout << "L1不为空,大小为" <<L1.size() <<endl;
}
//重新指定大小
//L1.resize(10);
L1.resize(10, 232); // 用232填充,默认为0
printList(L1);
L1.resize(2);
printList(L1);
}
//总结
//判断是否为空 --empty
//返回元素个数 --size
//重新指定个数--resize
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<list>
/*
-push_back(elem) ;//在容器尾部加入一一个元素
- pop_back();//删除容器中最后- .个元素
- push_front(elem) ;//在容器开头插入-一个元素
-pop_front();//从容器开头移除第-一个元素
一insert (pos, elem) ;//在pos位置插elem元素的拷贝,返回新数据的位置。
- insert (pos, n, elem) ;//在pos位置插入n个elem数据,无返回值。
- insert (pos, beg, end) ;//在pos位置插入[beg, end)区间的数据,无返回值。
- clear();//移除容器的所有数据
- erase (beg, end) ;//删除[beg, end)区间的数据,返回下-一个数据的位置。
-erase(pos) ;//删除pos位置的数据,返回下- -个数据的位置。
-remove (elem) ;//删除容器中所有与elem值匹配的元素。
*/
void printList(const list<int> &L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int> L;
//尾插
L.push_back(10);
L.push_back(40);
L.push_back(70);
L.push_back(90);
//头插
L.push_front(100);
L.push_front(400);
L.push_front(700);
printList(L);
//尾删
L.pop_back();
printList(L);//700 400 100 10 40 70
//头删
L.pop_front();
printList(L);// 400 100 10 40 70
//inset 插入
list<int>::iterator it = L.begin();
L.insert(++it, 234); // 400 234 100 10 40 70
printList(L);
//删除
it = L.begin();
L.erase(++it);
printList(L); // 400 100 10 40 70
//移除
L.push_back(9000);
L.push_back(9000);
L.push_back(9000);
printList(L);// 400 100 10 40 70 9000 9000 9000
L.remove(9000);
printList(L); // 400 100 10 40 70
//清空
L.clear();
printList(L);
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:
//●尾插--push_back
//●尾删--pop_back
//●头插--push_front
//●头删--pop.front
//●插入--insert
//●删除--erase
//●移除--remove
//●清空--clear
#include<iostream>
using namespace std;
#include<list>
void test01()
{
list<int> L;
//尾插
L.push_back(10);
L.push_back(40);
L.push_back(70);
L.push_back(90);
//L[0]; 不可以用[]访问容器中的元素
//L.at(0); 不可以用at方式访问list容器中的元素
//原因是list本质链表,不是用连续存储数据,迭代器也是不支持随机访问的
cout << "第一个元素" << L.front() << endl;
cout << "最后一个元素" << L.back() << endl;
//验证迭代器是不支持随机访问
list<int>::iterator it = L.begin();
it++; //支持双向
it--;
/*it += 1;
it = it + 1;*/ //不支持随机访问
}
int main()
{
test01();
system("pause");
return 0;
}
//总结:
//●list容器中不可以通过[]或者at方式访问数据
//●返回第一个元素--front
//●返回最后一个元素--back
#include<iostream>
using namespace std;
#include<list>
void printList(const list<int> &L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int compare(int num1, int num2)
{
//降序 就让第一个数>第二个数
return num1 > num2;
}
void test01()
{
list<int> L;
L.push_back(20);
L.push_back(50);
L.push_back(23);
L.push_back(42);
cout << "反转前" << endl;
printList(L);
cout << "反转后" << endl;
L.reverse();
printList(L);
//所有不支持随机访问迭代器的容器,不可以用标准算法
//不支持随机访问迭代器的容器,内部会提供对应一-些算法
//sort (L1. begin(),L1. end()) ;
cout << "从小到大排序后" << endl;
L.sort(); //默认从小到大排序,升序
printList(L);
cout << "从大到小排序后" << endl;
L.sort(compare);
printList(L);
}
int main() {
test01();
system("pause");
return 0;
}
//总结:
//●反转 -- reverse
//●排序 --sort(成员函数)
#include<iostream>
using namespace std;
#include<list>
class Person
{
public:
Person(string name, int age, int hight)
{
this->m_name = name;
this->m_age = age;
this->m_hight = hight;
}
string m_name;
int m_age;
int m_hight;
};
void printPerson(list<Person>& L)
{
for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
{
//*it和it->一样的
cout << "姓名:" << (*it).m_name << " 年龄:" << it->m_age << " 身高:" << it->m_hight << endl;
}
}
bool compare(Person &p1 , Person &p2)
{
if (p1.m_age == p2.m_age)
{
//按身高降序
return p1.m_hight > p2.m_hight;
}
else
{
//按年龄升序
return p1.m_age < p2.m_age;
}
}
void test01()
{
list<Person> L;
//准备数据.
Person p1("刘备",35, 175);
Person p2("曹操", 45, 180);
Person p3("孙权", 40, 170);
Person p4("赵云", 25, 190);
Person p5("张飞",35, 160);
Person p6("关羽", 35, 200);
L.push_back(p1);
L.push_back(p2);
L.push_back(p3);
L.push_back(p4);
L.push_back(p5);
L.push_back(p6);
printPerson(L);
L.sort(compare);
cout << "排序后" << endl;
printPerson(L);
}
int main() {
test01();
system("pause");
return 0;
}
//总结:
//●对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序
//●高级排序只是在排序规则上再进行 - -次逻辑规则制定,并不复杂
评论列表 (0 条评论)