c++学习:容器stack栈+queue+map(简易输入法)+deque

目录

stack

模板原型

头文件

模板的成员类型和成员对象和成员函数

栈类模板的容器对象

实例

queue

模板原型

头文件

模板的成员类型和成员对象和成员函数

队列类模板的容器对象

实例

map

模板原型

头文件

模板的成员类型和成员对象和成员函数

关联类模板的容器对象

实例1

实例2

deque

模板原型

头文件

模板的成员类型和成员对象和成员函数

双端队列类模板的容器对象

实例1


stack

std::stack 类是一种容器适配器,是栈——先进后出数据结构

模板原型

template<
    class T,
    class Container = std::deque<T>
> class stack;

头文件

#include <stack>

模板的成员类型和成员对象和成员函数

std::stack - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/stack

栈类模板的容器对象

//实例化一个 栈类模板的容器对象
std::stack<std::string> stack;

实例

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    //栈这种数据结构----先进后出
    //实例化一个 栈类模板的容器对象  栈的成员类型是string
    std::stack<std::string> stack;

    //入栈 --向栈顶插入元素
    stack.push("zhang3");
    stack.push("li4");
    stack.push("wang5");
    stack.push("lao6");

    //出栈
    while(1)
    {
        //出栈的数据如何获取数据
        cout<<stack.top()<<endl;
        //删除--删除栈顶
        stack.pop();
        //判断栈是否为空
        if(stack.empty() == true)
            break;
    }

    return 0;
}

queue

std::queue 类是一种容器适配器,是队列——先进先出数据结构

模板原型

template<
    class T,
    class Container = std::deque<T>
> class queue;

头文件

#include <queue>

模板的成员类型和成员对象和成员函数

std::queue - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/queue

队列类模板的容器对象

    //实例化 队列类模板的对象
    std::queue<std::string> queue;

实例

#include <iostream>
#include <queue>

using namespace std;

int main()
{
    //实例化 队列类模板的对象
    std::queue<std::string> queue;

    //入队--尾插  ---两端操作
    queue.push("zhang3");
    queue.push("li4");
    queue.push("wang5");
    queue.push("lao6");

    //判断队列是否为空
    while(!queue.empty())
    {
        //出队之前,先获取数据--头部数据
        cout<<queue.front()<<endl;
        //出队,头部数据删除
        queue.pop();
    }

    return 0;
}

map

std::map 是一种有序关联容器,它包含具有唯一键的键值对。键之间以比较函数 Compare 排序。搜索、移除和插入操作拥有对数复杂度。map 通常实现为红黑树。

模板原型

template<
    class Key,
    class T,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<std::pair<const Key, T>>
> class map;

头文件

#include <map>

模板的成员类型和成员对象和成员函数

std::map - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/map

关联类模板的容器对象

    //实例化一个关联容器类模板的对象
    //数据格式:  键值对的方式存在   key-value
    std::map<long int,std::string> map;

    //实例化一个键值对类模板的对象
    std::pair<long int,std::string> data(200,"lao6");

实例1

#include <iostream>
#include <map>

using namespace std;

int main()
{
    //实例化一个关联容器类模板的对象
    //数据格式:  键值对的方式存在   key(int)-value(string)
    std::map<long int,std::string> map;

    //插入数据  operator[]  访问或插入指定的元素
    map[100] = "zhang3";
    map[120] = "li4";
    map[122] = "wang5";

    //实例化一个键值对类模板的对象
    std::pair<long int,std::string> data(200,"lao6");
    //insert插入数据
    map.insert(data);

    //访问
    cout<<map[120]<<endl;

    //通过迭代器遍历整个容器
    std::map<long int,std::string>::iterator it;
    for(it=map.begin(); it!=map.end(); it++)
    {
        //it->first 得到键   second 得到值
        cout<<it->first<<"\t"<<it->second<<endl;
    }

    return 0;
}

实例2  简易输入法

#include <iostream>
#include <map>
using namespace std;

int main()
{
    //实现一个简单的拼音中文输入法
    std::map<string,string> map;

    //插入数据
    map["a"] = "啊阿錒吖嗄";
    map["b"] = "吧把不被表";
    map["c"] = "从成层出插";
    map["ni"] = "你尼呢腻拟";

    while(1)
    {
        string input;
        cin>>input;//输入key

        string value = map[input];//获取对应key的value

        //汉字字符串--->utf-8占3个字节   //遍历value 
        for(int i=0; i<value.size()/3; i++)//汉字的个数
        {
            cout<<i+1<<" "<<value.substr(3*i,3)<<"\t";
        }cout<<endl;

        //根据输入数字显示对应的汉字
        int num=0;
        cin>>num;
        cout<<value.substr(3*(num-1),3)<<endl;
    }

    return 0;
}

deque

std::deque(double-ended queue,双端队列)是有索引的序列容器,它允许在它的首尾两端快速插入及删除。另外,在 deque 任一端的插入或删除不会使指向其余元素的指针或引用失效。等于队列和栈的结合体

模板原型

template<
    class T,
    class Allocator = std::allocator<T>
> class deque;

头文件

#include <deque>

模板的成员类型和成员对象和成员函数

std::deque - cppreference.comicon-default.png?t=N7T8https://zh.cppreference.com/w/cpp/container/deque

双端队列类模板的容器对象

std::deque<int> deque;

实例1

#include <iostream>
#include <deque>

using namespace std;

int main()
{
    std::deque<int> deque;

    //实现队列功能
    deque.push_back(10);
    deque.push_back(20);
    deque.push_back(30);
    deque.push_back(40);

    //遍历
    for(int i = 0; i<deque.size();i++)
    {
        cout<<deque[i]<<endl;
    }
    //也可以使用迭代器遍历
    for(std::deque<int>::iterator it = deque.begin();it!=deque.end();it++)
    {
        cout<<*it<<endl;
    } 

    //出队 头删
    //判断是否为空
    while(!deque.empty())
    {
        //先访问头部数据
        cout<<deque.front()<endl;
        deque.pop_front();//
    }

    return 0;
}

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 09:36:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 09:36:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 09:36:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 09:36:01       20 阅读

热门阅读

  1. Mac上安装tensorflow介绍留存

    2024-01-10 09:36:01       37 阅读
  2. 程序员的面试技巧

    2024-01-10 09:36:01       38 阅读
  3. 时间格式化插件(dayjs)

    2024-01-10 09:36:01       35 阅读
  4. The ultimate BMW scan tool on the market!

    2024-01-10 09:36:01       37 阅读
  5. 力扣-278. 第一个错误的版本

    2024-01-10 09:36:01       37 阅读
  6. xopen 一个高效压缩和解压缩python库碾压gzip

    2024-01-10 09:36:01       25 阅读
  7. apache2的虚拟主机的配置

    2024-01-10 09:36:01       32 阅读
  8. DevOps|产研运协作工具链上的皇冠-项目管理工具

    2024-01-10 09:36:01       34 阅读
  9. Leetcode 2697. 字典序最小回文串

    2024-01-10 09:36:01       35 阅读