zoukankan      html  css  js  c++  java
  • 3.Queues(队列)

    一.概述

    C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构,与stack刚好相反

    二.常用API

    back() 返回最后一个元素
    empty() 如果队列空则返回真
    front() 返回第一个元素
    pop() 删除第一个元素
    push() 在末尾加入一个元素
    size() 返回队列中元素的个数

    三.示例Demo

    #include <iostream>
    #include <stdlib.h>
    #include <queue>
    
    using namespace std;
    #pragma warning(disable:4996)
    
    
    
    struct Teacher {
        char name[20];
        int age;
    };
    
    
    void printQueue(queue<Teacher *> &v) {
        
        while (!v.empty())
        {
            Teacher *teacher = v.front();
            cout << "Teacher, name is: " << teacher->name << ", age is: " << teacher->age << endl;
            cout << "current remain num is: " << v.size() << endl;
            v.pop();
        }
    
    }
    
    
    int main() {
        
        
        Teacher *t1 = (Teacher *)malloc(sizeof(Teacher));
        Teacher *t2 = (Teacher *)malloc(sizeof(Teacher));
        Teacher *t3 = (Teacher *)malloc(sizeof(Teacher));
    
        strcpy(t1->name,"jack");
        t1->age = 11;
        strcpy(t2->name,"mike");
        t2->age = 22;
        strcpy(t3->name,"tom");
        t3->age = 33;
    
    
        queue<Teacher *> v;
        v.push(t1);
        v.push(t2);
        v.push(t3);
    
        printQueue(v);
    
        free(t1);
        free(t2);
        free(t3);
    
    
        system("pause");
    
        return 0;
    }

    运行结果:

    Teacher, name is: jack, age is: 11
    current remain num is: 3
    Teacher, name is: mike, age is: 22
    current remain num is: 2
    Teacher, name is: tom, age is: 33
    current remain num is: 1

  • 相关阅读:
    用jQuery实现的简单柱状统计图
    转:Mac下配置eclipse的pydev
    准备做些东西
    UIImageJPEGRepresentation 和 UIImagePNGRepresentation
    UITableView,UITableViewCell,UIPickerView,UISearchBar
    UITableViewCell的显示格式
    js 数据类型检测
    data-xxx 属性的作用是什么
    TCP连接篇
    DNS篇
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/7159347.html
Copyright © 2011-2022 走看看