• c++queue容器


    queue 模板类的定义在<queue>头文件中。
    与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
    型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
    定义queue 对象的示例代码如下:
    queue<int> q1;
    queue<double> q2;

    queue 的基本操作有:
    入队,如例:q.push(x); 将x 接到队列的末端。
    出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
    访问队首元素,如例:q.front(),即最早被压入队列的元素。
    访问队尾元素,如例:q.back(),即最后被压入队列的元素。
    判断队列空,如例:q.empty(),当队列空时,返回true。
    访问队列中的元素个数,如例:q.size()

     1 #include<iostream>
     2 using namespace std;
     3 #include<queue>
     4 #include<algorithm>
     5 
     6 //队列中基本数据模型
     7 void main61()
     8 {
     9     queue<int> q;
    10     q.push(1);
    11     q.push(2);
    12     q.push(3);
    13     q.push(4);
    14 
    15 
    16     cout << "队头元素:" << q.front() << endl;
    17     cout << "队列大小:" << q.size() << endl;
    18 
    19     while (!q.empty())
    20     {
    21         cout << q.front() << " ";
    22         q.pop();
    23 
    24     }
    25 }
    26 
    27 //队列的算法 和 数据类型的分离
    28 class Teacher
    29 {
    30 public:
    31     int age;
    32     char name[32];
    33 
    34 public:
    35     void printT()
    36     {
    37         cout << "age: " << age << endl;
    38     }
    39 };
    40 //队列中对象数据模型
    41 void main62()
    42 {
    43     Teacher t1, t2, t3;
    44     t1.age = 31;
    45     t2.age = 32;
    46     t3.age = 33;
    47     queue<Teacher> q;
    48     q.push(t1);
    49     q.push(t2);
    50     q.push(t3);
    51 
    52     while (!q.empty())
    53     {
    54         Teacher temp = q.front();
    55         temp.printT();
    56         q.pop();
    57     }
    58 
    59 
    60 
    61 }
    62 //队列中指针数据模型
    63 void main63()
    64 {
    65     Teacher t1, t2, t3;
    66     t1.age = 31;
    67     t2.age = 32;
    68     t3.age = 33;
    69     queue<Teacher *> q;
    70     q.push(&t1);
    71     q.push(&t2);
    72     q.push(&t3);
    73 
    74     while (!q.empty())
    75     {
    76         Teacher *temp = q.front();
    77         temp->printT();
    78         q.pop();
    79     }
    80 
    81 
    82 
    83 }
    84 int main()
    85 {
    86     main63();
    87 
    88 
    89 
    90     system("pause");
    91     return 0;
    92 }
  • 相关阅读:
    迭代器、生成器、内置函数
    函数
    文件操作
    使用 java 做爬虫的简单例子
    关于 spring 使用 mongodb 的 mongotemplate 对象操作数据库,对象注入问题(即该对象能否正常的调用相应的CRUD方法来处理数据)
    eclipse 使用 “全局搜索” 失灵的解决方法
    使用 Qrcode 生成中间带 logo 的二维码!
    将 BufferedImage 对象图片,转成 Base64 编码给前端<img src="编码"/>展示图片用
    使用 Qrcode 生成二维码
    链接转化成二维码
  • 原文地址:https://www.cnblogs.com/ymj11/p/13810190.html
Copyright © 2020-2023  润新知