stack栈容器,queue队容器,list容器,

stack栈容器: 

stack他是一种栈容器,不允许有遍历行为,每次只能通过弹出栈来拿到栈顶的元素。

栈容器没有迭代器,因为不具备遍历功能所以没有迭代器。

所有元素必须符合后进先出

#include<iostream>
#include<stack>
using namespace std;
void test()
{
    stack<int>v;
    v.push(10);
    v.push(20);
    v.push(30);
    v.push(40);
    v.push(50);             
    stack<int>v1(v);        
    stack<int>v2 = v1;
    v2.pop();
    v2.pop();
    v2.pop();
    cout << v2.top() << endl;     
    if (v2.empty())
    {
    	cout << "为空" << endl;
    }
    else
    {
    cout << "非空" << endl;
    }
    cout << v2.size() << endl;
    v2.pop();
    v2.pop();
    if (v2.empty())
    {
    	cout << "为空" << endl;
    }
    else
    {
    	cout << "非空" << endl;
    }
    cout << v2.size() << endl;
}
int main()
{
    test();
    return 0;
}




运行结果:
20
非空
2
为空
0
请按任意键继续. . .



想要查看元素必须从栈顶开始弹出,弹出完毕stack已然为空。(类似于一个黑盒子,只有弹出才可以查看)

queue队容器:

queue队列容器的基本概念:Queue是一种先进先出的数据结构,他有两个出口,允许从一端添加元素,而从另外一段移除元素(类似于公交车,只能从前门进,后门下)。

Queue所有的元素必须符合先进先出的特点,也类似于一个黑盒子,顶端top元素才可以被使用。Queue不提供迭代器,也没有便利功能。

这里画一个栈和队列的图像:

                                         

            栈                                                                                                           队列

栈只允许看到栈顶的元素                            队列只允许看到第一个(front)和最后一个(back)但是可以弹出

的只有front

#include<iostream>
#include<queue>
using namespace std;
void test()
{
    queue<int>v;
    v.push(10);
    v.push(20);
    v.push(30);
    v.push(40);
    v.push(50);
    while (!v.empty())
    {
    	cout << "队头" << v.front() << endl;     //查看
    	cout << "队尾" << v.back() << endl;
    	v.pop();            //弹出
    }
    cout << v.size();
}
int main()
{
    test();
    return 0;
}

list容器:

list容器的基本概念:

 

常数时间的意思就是之操作一次,就是拿到第五个元素,一步操作就好。而队列栈,要想拿到第五个元素就要通过反复的弹出操作。

list和vector是两个最常被使用的容器

list是一个双向链表

list迭代器不能像vector迭代器一样普通指针就可以作为迭代器,因为list是不连续的空间,  必须使用有特殊能力的迭代器,类似于在链表中,自己定义的链表结点,next不但可以指向下一个结点的地址域,还有可以指向值域。

这个 的话是因为list使用的是双向链表,vector使用的是连续的空间,当你添加新元素的时候,很有可能导致空间不够,那么就会重新寻找一块空间那么,指向原有空间的所有迭代器将失效,但是list使用的是链式存储结构不会这样的情况。

list不但是一个链表,而且双向循环链表

list是一个具有头结点(空结点)的链表。

//证明list是一个循环链表

#include<iostream>
#include<list>
using namespace std;
void test()
{
    list<int>v;
    for (int i = 0; i < 10; ++i)
    {
    	v.push_back(i);
    }
    list<int>::_Nodeptr it = v._Myhead->_Next;
    for (int i = 0; i < v._Mysize*2; ++i)
    {
       	cout << it->_Myval;
    	it = it->_Next;
    	if (it == v._Myhead)
    	{
        	it = it->_Next;
    		cout << endl;
    	}
    }
}
int main()
{
    test();
    return 0;
}

结果:
0123456789
0123456789
请按任意键继续. . .


list迭代器不支持随机访问

#include<iostream>
#include<list>
using namespace std;
void printfs(list<int>& q)                      //打印list
{
    for (list<int>::iterator it = q.begin(); it != q.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
void printfs2(list<int>& q)                      //打印list
{
    for (list<int>::reverse_iterator it = q.rbegin(); it != q.rend(); ++it)      //reverse_iterator逆向打印迭代器
    {
    	cout << *it << " ";
    }
    cout << endl;
}
void test1()
{
    list<int>v;   //模板默认构造
    for (int i = 0; i < 10; ++i)
    {
    	v.push_back(i);
    }
    list<int>v1(v.begin(), v.end());       //提供区间构造
    list<int>v2(10, 20);       //值构造
    printfs(v);       //正向打印           
    list<int>v3(v1);      //拷贝构造
    printfs2(v3);             //逆向打印
}
int main()
{
    test1();
    return 0;
}
#include<iostream>
#include<list>
using namespace std;
void printfs(list<int>& q)                      //打印list
{
    for (list<int>::iterator it = q.begin(); it != q.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
void test2()
{
    //remove(elem)删除容器中所有与elem相同的数据
    list<int>v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(2);
    v.push_back(5);
    v.push_back(4);
    v.push_back(2);
    v.remove(2);
    printfs(v);
}
int main()
{
    test2();
    return 0;
}

结果:
1 5 4
请按任意键继续. . .

这一组我就不测了,基本与vector都相同

回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;
bool func(int a,int b)
{
    return  a > b;
}
void printfs(list<int>& q)                      //打印list
{
    for (list<int>::iterator it = q.begin(); it != q.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
void test3()
{
    list<int>v;
    for (int i = 0; i < 10; ++i)
    {
    	v.push_back(i);
    }
    printfs(v);
    v.reverse();     //反转
    printfs(v);
    //sort(v.begin(), v.end());   这样直接排序是会报错的,因为所有不支持随机访问的迭代器无法使用这种排序
    v.sort();         //可以使用这种排序,这是由类内部提供的一种排序
    printfs(v);
    v.sort(func);     //还有就是继续使用提供的算法的话那么就要使用回调函数
    printfs(v);
}
int main()
{
    test3();
    return 0;
}


结果:

0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 4 3 2 1 0
请按任意键继续. . .

对于自定义数据类型的排序输出 

#include<iostream>
#include<list>
using namespace std;
class person
{
public:
    person(string name, int ages)
    {
    	this->ages = ages;
    	this->myname = name;
    }
    string myname;
    int ages;
};
bool func(person& a, person& b)        //类比于int型数据,这里的参数是   类型+对象
{
    if (a.ages > b.ages)
    	return true;
    else
    	return false;
}
void printfs(list<person>&v)
{
    for (list<person>::iterator it = v.begin(); it != v.end(); ++it)
    {
    	cout << ((*it).myname).c_str() << " " << ((*it).ages) << endl;        //关于这一块无法将(*it).name直接输出,而是要转换成char*的格式才可以输出,不知道为什么
//最后我终于知道为什么了,因为没有包含头文件string,那么我查阅发现string.h和string类没有半毛钱关系,具体操作看后面
    }
}
void test4()
{
	
    list<person>v;
    person p1("高渐离", 18);
    person p2("阿珂", 22);
    person p3("嬴政", 34);
    person p4("白起", 28);
    person p5("百里守约", 16);
    person p6("安琪拉", 10);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    v.push_back(p6);
    //a按照年龄大小对元素进行输出
    v.sort(func);       //对于自定义的书记类型必须使用自己写的回调函数
    printfs(v);
	
}
int main()
{
    test4();
    return 0;
}

结果:
嬴政 34
白起 28
阿珂 22
高渐离 18
百里守约 16
安琪拉 10
请按任意键继续. . .


#include<iostream>
#include<list>
#include<string>
using namespace std;
void printfs(list<int>& q)                      //打印list
{
    for (list<int>::iterator it = q.begin(); it != q.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
class person
{
public:
    person(string name, int ages, int tall)
    {
    	this->ages = ages;
    	this->myname = name;
    	this->tall = tall;
    }
    string myname;
    int ages;
    int tall;
};
bool func(person& a, person& b)        //类比于int型数据,这里的参数是   类型+对象
{
    if (a.ages == b.ages)
    {
    	return a.tall > b.tall;       //如果两个对象的年龄相当就按谁的个子高谁在前面排序
    }
    return a.ages > b.ages;            //否则就按年龄大的在前面
}
void printfs(list<person>&v)
{
    for (list<person>::iterator it = v.begin(); it != v.end(); ++it)
    {
    	cout << (*it).myname << "\t" << (*it).ages << "\t" << it->tall << endl;
    }
}
void test4()
{
	
    list<person>v;
    person p1("高渐离", 18,180);
    person p2("阿珂", 22,120);
    person p3("嬴政", 34,130);
    person p4("白起", 28,150);
    person p5("明世隐", 16,187);
    person p6("安琪拉", 10, 155);
    person p7("黄忠", 10, 185);
    person p8("橘子", 10, 142);
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);
    v.push_back(p6);
    v.push_back(p7);
    v.push_back(p8);
    //a按照年龄大小对元素进行输出
    v.sort(func);       //对于自定义的书记类型必须使用自己写的回调函数
    printfs(v);
	
}
int main()
{
    test4();
    return 0;
}


结果:
嬴政    34      130
白起    28      150
阿珂    22      120
高渐离  18      180
明世隐  16      187
黄忠    10      185
安琪拉  10      155
橘子    10      142
请按任意键继续. . .

对自定义数据类型再进行remove操作,需要修改==运算符

#include<iostream>
#include<list>
#include<string>
using namespace std;
void printfs(list<int>& q)                      //打印list
{
    for (list<int>::iterator it = q.begin(); it != q.end(); ++it)
    {
    	cout << *it << " ";
    }
    cout << endl;
}
class person
{
public:
    person(string name, int ages, int tall)
    {
    	this->ages = ages;
    	this->myname = name;
    	this->tall = tall;
    }
    bool operator==(const person p)             //关于返回值为什么是bool类型,由remove底层函数可以看出if (*_First == _Val)
//这里需要加上const否则有时可能编译不通过,可能和版本还有编译器有关吧,但是加上就一定可以通过,因为我们进行的是对底层符号的重载,防止害怕改变在函数内通过p修改数据
    {                                      //这里既然是对==的重载那么就必然返回等号所需要知道的东西,那就是两个对象相等或者不等
	if (this->ages == p.ages&&this->myname == p.myname&&this->tall == p.tall)
	{
    	    return true;
    	}
	    else
    	    return false;
    }
    string myname;
    int ages;
    int tall;
};
bool func(person& a, person& b)        //类比于int型数据,这里的参数是   类型+对象
{
    if (a.ages == b.ages)
    {
    	return a.tall > b.tall;       //如果两个对象的年龄相当就按谁的个子高谁在前面排序
    }
        return a.ages > b.ages;            //否则就按年龄大的在前面
}
void printfs(list<person>&v)
{
    for (list<person>::iterator it = v.begin(); it != v.end(); ++it)
    {
    	cout << (*it).myname << "\t" << (*it).ages << "\t" << it->tall << endl;
    }
}

void test4()
{
	
	list<person>v;
	person p1("高渐离", 18,180);
	person p2("阿珂", 22,120);
	person p3("嬴政", 34,130);
	person p4("白起", 28,150);
	person p5("明世隐", 16,187);
	person p6("安琪拉", 10, 155);
	person p7("黄忠", 10, 185);
	person p8("橘子", 10, 142);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	v.push_back(p6);
	v.push_back(p7);
	v.push_back(p8);
	//a按照年龄大小对元素进行输出
	v.sort(func);       //对于自定义的书记类型必须使用自己写的回调函数
	printfs(v);
	v.remove(p6);
	v.remove(p5);
	v.remove(p4);
	v.remove(p2);
	v.remove(p8);
	//假如我们要使用remove来删除p6,这个肯定是错误的,这个就要查看remove的底层定义了
	//void remove(const _Ty& _Val)
	//{	// erase each element matching _Val
	//	iterator _Val_it = end();

	//	for (iterator _First = begin(); _First != end();)
	//	if (*_First == _Val)
	//	if (_STD addressof(*_First) == _STD addressof(_Val))
	//		_Val_it = _First++;
	//我们会发现在底层有这么一句话if (*_First == _Val),用你的自定义类型和v.begin()比较,
	//两者都是对象(要是基本类型就可以这样比较),但是自定义类型系统不知道如何比较,
	//所以如果要使用remove删除自定义数据类型,就必须重写 ==
	cout << "===============================================" << endl;
	printfs(v);
}

int main()
{
    test4();
    return 0;
}



结果:
嬴政    34      130
白起    28      150
阿珂    22      120
高渐离  18      180
明世隐  16      187
黄忠    10      185
安琪拉  10      155
橘子    10      142
===============================================
嬴政    34      130
高渐离  18      180
黄忠    10      185
请按任意键继续. . .

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值