目录
栈的数据结构特性是:先入后出,后入先出,只能从栈顶插入、删除和访问。可将栈视为一叠盘子,最后叠上去的盘子首先被取下来,而不能从中间或底部取出盘子。
stack
stack(const stack &stk);//拷贝构造函数
stack& operator=(const stack &stk);//重载等号操作符
push(elem);//向栈顶添加元素
pop();//从栈顶移除第一个元素
top();//返回栈顶元素
stack大小操作:
empty();//判断堆栈是否为空
size(0);//返回栈的大小
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- void test()
- {
- stack<int> s;
- s.push(10);
- s.push(20);
- s.push(30);
- s.push(40);
- cout<< "栈的大小为:"<< s.size() << endl;
- while(!s.empty())//判断栈是否为空
- {
- cout << "栈顶元素为:" << s.top()<< endl;
- s.pop();//移除栈顶元素
- }
-
- cout<< "栈的大小为:"<< s.size() << endl;
- }
-
- int main()
- {
- test();
-
- return 0;
- }
编译运行

队列的数据结构特性是:先进先出,允许在开头删除,在末尾插入,类似排队系统。

队列容器允许从一端新增元素,从另一端移除元素
队列中只有对头和队尾才可以被外界使用,因此队列不允许有遍历行为
队列中进数据称为入队,出数据称为出队
queue
queue(const queue &que); //拷贝构造函数
queue& operator=(const queue &que); //重载等号操作符
push(elem);//往队尾添加元素
pop();//从队头移除第一个元素
back();//返回最后一个元素
front();//返回第一个元素
empty();判断是否尾空
size();//返回大小
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- void test()
- {
- queue<int> q;
- q.push(10);
- q.push(20);
- q.push(30);
- q.push(40);
-
-
- while(!q.empty())//判断栈是否为空
- {
- cout << "对头元素为:" << q.front()<< endl;
-
- cout << "对尾元素为:" << q.back()<< endl;
-
- q.pop();//移除队头
- }
- cout<< q.size() << endl;
- }
-
- int main()
- {
- test();
-
- return 0;
- }
编译运行

list是双向链表的一个泛化容器,它的数据元素可通过链表指针串接成逻辑意义上的线性表。不同于采用线性表顺序存储结构的vector和deque容器,list双向链表中任一位置的元素查找、插入和删除,都具有高效的常数阶算法时间复杂度
list的优点:
采用动态存储分配,不会造成内存浪费和溢出
链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素list的缺点:
链表灵活,但是空间(指针域)和时间(遍历) 额外耗费较大List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的.
list
list(beg,end);//构造函数将[beg,end)区间中的元素拷贝给本身
list(n,elem);//构造函数将n个elem拷贝给本身。
list(const list &lst);//拷贝构造函数。
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- void print(const list<int>&L)
- {
- for(list<int>::const_iterator it = L.begin();it!=L.end();it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- void test()
- {
-
- list<int>L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- print(L1);
-
- list<int>L2(L1.begin(),L1.end());
- print(L2);
-
- list<int>L3(L2);
- print(L3);
-
- list<int>L4(10,40);
- print(L4);
-
-
- }
-
- int main()
- {
- test();
-
- return 0;
- }
编译运行

assign(beg,end);//将[begend)区间中的数据拷贝赋值给本身
assign(n,elem);//将n个elem拷贝赋值给本身。
list& operator=(const list &lst);//重载等号操作竹
swap(lst)://将lst与本身的元素互换
- void print(const list<int>&L)
- {
- for(list<int>::const_iterator it = L.begin();it!=L.end();it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- void test()
- {
-
- list<int>L1;
- L1.push_back(10);
- L1.push_back(20);
- L1.push_back(30);
- L1.push_back(40);
- print(L1);
-
- list<int>L2(10,40);
- print(L2);
-
- cout << "交换后: "<< endl;
- L2.swap(L1);
-
- print(L1);
- print(L2);
-
- }
编译运行

size();//返回容器中的个数
empty();//判断容器是否为空
resize(num);//重新指定容器的长度为num,若容器变长,则以默认值填充新位置
//如果容器变短,则末尾超出容器长度的元素被制除。
resize(num,elem);//重新指定容器的长度为num,若容器变长,则以elem值填充新位置 //如果容器变短,则末尾超出容器长度的元素被删除。
push_back(elem);//在容器尾部加入一个元素
pop_back0)://删除容器中最后一个元素
push front(elem)://在容器开头插入一个元妻
pop_front()://从容器开头移除第一个元素
insert(pos,elem)://在pos位置插elem元素的拷贝,返回新数据的位置
insert(pos,n,elem)://在pos位置插入n个elem数据,无返回值
insert(pos,beg,end);//在pos位置插入[begend)区间的数据,无返回值
clear();//移除容器的所有数据
erase(beg,end)://删除[begend)区间的数据,返回下一个数据的位置
erase(pos)://删除pos位置的数据,返回下一个数据的位置
remove(elem)://删除容器中所有与elem值匹配的元素
front();//返回第一个元素
back();//返回最后一个元素
void test()
{
listL1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);cout << "第一个元素: " << L1.front() << endl;
cout << "最后一个元素: " << L1.back() << endl;//L1[0]不可以用[]访问list容器的元素
//L1.at(0) 不可以用at方式访问list容器里的元素
//原因是list本质链表 不是用连续线空间存储数据,迭代器也不支持随机访问
list::iterator it = L1.begin(); it++;//支持双向
it--;
// it = it + 1;//不支持随机访问
}
reverse();//反转链表
sort();//链表排序(成员函数)
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
-
- void print(const list<int>&L)
- {
- for(list<int>::const_iterator it = L.begin();it!=L.end();it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
-
- bool myc(int v1,int v2)
- {
- //降序 就让第一个数大于第二个数
- return v1 > v2;
- }
-
-
- void test01()
- {
- list<int>L1;
- L1.push_back(20);
- L1.push_back(10);
- L1.push_back(40);
- L1.push_back(30);
- L1.push_back(70);
-
- print(L1);
-
- L1.reverse();
- print(L1);
- }
-
- void test02()
- {
- list<int>L1;
- L1.push_back(20);
- L1.push_back(10);
- L1.push_back(40);
- L1.push_back(30);
- L1.push_back(70);
-
- print(L1);
-
- //所有不支持随机访问迭代器的容器,不可以用标准算法
- //不支持随机访迭代器的容器,内部会提供对应的一些算法
- //sort(L1.begin(),L1.end());
- L1.sort();
- print(L1);
-
- //L1.sort(myc);降序排列
-
- }
-
- int main()
- {
- test01();
- cout << endl;
- test02();
- return 0;
- }
编译运行
