C++中的STL容器

C++中的STL容器

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
//基本通用
//	XXX.size()               返回元素数目
//  XXX.empty()       		判断是否为空
//  XXX.clear()           	清空所有元素
//每种容器类型都定义了自己的迭代器类型
using namespace std;

struct stu
{
    int age;
    int height;
    /*
    stu(int Age,int Height)//构造函数
    {
        age=Age,height=Height;
    }*/
    void print()//自定义函数
    {
        cout<<age<<' '<<height<<endl;
    }
    friend bool operator < (stu a,stu b)//重载小于运算符,重载运算符内容可以自行百度慢慢学习,C++可以重载很多运算符,非常有用。
    {
        if(a.age==b.age)
            return a.height<b.height;
        return a.age<b.age;
    }
};
int main()
{
    //STL中的容器是被封装过的泛型数据结构。
    //容器名称<数据类型> 变量名称;
    cout<<"vector"<<endl;
    vector<int> v;
    /*
    向量容器,也叫动态数组,其本质是连续的数组,并可以动态的添加元素,
    从内部实现看,其在申明时会分配1个单位的空间,在添加过程中如果不够,会在另一块内存开辟两倍的空间并把内容copy过去,
    */
    v.push_back(1);//尾部追加
    v.push_back(2);//尾部追加
    cout<<v[0]<<" "<<v[1]<<endl;//可用[]访问
	system("pause");
    //通过迭代器访问vector的元素
	vector<int>::iterator iter; //这条语句定义了一个名为iter的变量,它的数据类型是由vector<int>定义的iterator类型。
	for(iter=v.begin();iter!=v.end();++iter){
		cout<<*iter<<" "; //使用 * 访问迭代器所指向的元素
	}
	cout<<endl;
	system("pause");



	cout<<endl<<"stack"<<endl;
    stack<string> stk;
    //堆栈,先进后出的结构,本质是vector的进一步封装
    stk.push("abc");//通过push压入元素
    stk.push("bcd");
    cout<<stk.top()<<' ';//通过top访问栈顶元素
    stk.pop();//通过pop把栈顶元素弹出
    cout<<stk.top()<<endl;
	system("pause");



	cout<<endl<<"queue"<<endl;
    queue<int> q;
    //队列,先进先出的结构,也是vector的封装
    q.push(1);
    q.push(2);
    if(!q.empty())
        cout<<"queue is not empty."<<endl;
    cout<<q.front()<<' ';//通过front访问队首元素
    q.pop();
    cout<<q.front()<<endl;
	system("pause");



	cout<<endl<<"priority_queue"<<endl;
    stu peo[5]={{17,180},{16,170},{18,190},{17,140},{18,180}},stuTemp;
    priority_queue<stu> pq;
    //优先队列,根据所使用数据类型的权值来进行排序的队列,不再是单纯的先进先出
    for(int i=0;i<5;i++)
        pq.push(peo[i]);
    while(!pq.empty())
    {
        stuTemp=pq.top();
        stuTemp.print();
        pq.pop();//priority_queue的队首用top获得
    }
	system("pause");



	cout<<endl<<"map"<<endl;
	map<string,int> mapp;
	//Map是一个以键值对的形式存储的结构,键值对的值可以为很多种,如string字符串,int整型,double 浮点型, char 字符
	//不过注意,键本身是不能被修改的,除非删除。 键值是可以修改的(删除数据是需要使用迭代器)
	string str[5]={"aaa","bbb","ccc","ddd","eee"};
	for(int i=0;i<5;i++){
		mapp[str[i]]=i;
	}
	//可以通过直接把键写在[]中 得出键值
	cout<<mapp["eee"]<<" "<<mapp["ddd"]<<" "<<mapp["ccc"]<<" "<<mapp["bbb"]<<" "<<mapp["aaa"]<<endl;
	system("pause");
	mapp.clear();
	for(int i=0;i<5;i++){
		mapp.insert(pair<string,int>(str[i],i));
	}
    //通过迭代器访问元素
	map<string,int>::iterator itmer; //这条语句定义了一个名为itmer的变量
	for(itmer=mapp.begin();itmer!=mapp.end();++itmer){
		cout<<itmer->second<<" ";
	}
	cout<<endl;
	system("pause");
	mapp.clear();
	for(int i=0;i<5;i++){
		mapp.insert(pair<string,int>(str[i],i));//用pair插入,pair是把一对元素组合的结构
	}
	mapp.erase("ccc");  //删除键为ccc的元素
	for(itmer=mapp.begin();itmer!=mapp.end();++itmer){
		cout<<itmer->second<<" ";
	}
	cout<<endl;
	system("pause");
	itmer=mapp.find("aaa");//查找键为aaa的
	if(itmer==mapp.end()){
		cout<<"no"<<endl;
	}
	else{
		cout<<"yes   "<<itmer->second<<endl;
	}
	system("pause");
	itmer=mapp.find("ccc");//查找键为ccc的
	if(itmer==mapp.end()){
		cout<<"no"<<endl;
	}
	else{
		cout<<"yes   "<<itmer->second<<endl;
	}
	system("pause");



	cout<<endl<<"set"<<endl;
	set<int>s;
	//同样的元素只能保存一个,可以用做筛选,把相同的数给去掉,只保留一个
	int nums[10]={1,1,3,2,4,5,6,7,7,9};
	for(int i=0;i<10;i++){
		s.insert(nums[i]);
	}
    //通过迭代器访问元素
	set<int>::iterator itser; //这条语句定义了一个名为itser的变量
	itser=s.find(1);
	if(itser==s.end()){
		cout<<"no"<<endl;
	}
	else{
		cout<<"yes"<<endl;
	}
	system("pause");
	for(itser=s.begin();itser!=s.end();itser++){//输出是有序的
		cout<<*itser<<" ";
	}
	cout<<endl;
	system("pause");
	s.erase(2);        //删除键值为2的元素
	itser=s.find(2);
	if(itser==s.end()){
		cout<<"no"<<endl;
	}
	else{
		cout<<"yes"<<endl;
	}

	for(itser=s.begin();itser!=s.end();itser++){
		cout<<*itser<<" ";
	}
	cout<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_39905917/article/details/83097346