zoukankan      html  css  js  c++  java
  • STL

    Queue简单介绍
    queue是队列容器。是一种“先进先出”的容器。


    queue是简单地装饰deque容器而成为另外的一种容器。


    #include <queue>

    queue对象的默认构造
    queue採用模板类实现,queue对象的默认构造形式:queue<T> queT;  如:
    queue<int> queInt;            //一个存放int的queue容器。

    queue<float> queFloat; //一个存放float的queue容器。

    queue<string> queString; //一个存放string的queue容器。

    ... //尖括号内还能够设置指针类型或自己定义类型。

    queue的push()与pop()方法 queue.push(elem); //往队尾加入元素 queue.pop(); //从队头移除第一个元素 queue<int> queInt; queInt.push(1);queInt.push(3); queInt.push(5);queInt.push(7); queInt.push(9);queInt.pop(); queInt.pop(); 此时queInt存放的元素是5,7,9 queue对象的拷贝构造与赋值 queue(const queue &que); //拷贝构造函数 queue& operator=(const queue &que); //重载等号操作符 queue<int> queIntA; queIntA.push(1); queIntA.push(3); queIntA.push(5); queIntA.push(7); queIntA.push(9); queue<int> queIntB(queIntA); //拷贝构造 queue<int> queIntC; queIntC = queIntA; //赋值 queue的数据存取  queue.back(); //返回最后一个元素  queue.front(); //返回第一个元素 queue<int> queIntA; queIntA.push(1); queIntA.push(3); queIntA.push(5); queIntA.push(7); queIntA.push(9); int iFront = queIntA.front(); //1 int iBack = queIntA.back(); //9 queIntA.front() = 11; //11 queIntA.back() = 19; //19 queue的大小  queue.empty(); //推断队列是否为空  queue.size(); //返回队列的大小 queue<int> queIntA; queIntA.push(1); queIntA.push(3); queIntA.push(5); queIntA.push(7); queIntA.push(9); if (!queIntA.empty()) { int iSize = queIntA.size(); //5 }


    demo
    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    void queueInit()
    {
    	queue<int> q;
    	q.push(1);
    	q.push(3);
    	q.push(5);
    
    	cout << "size of q: " << q.size() << endl;
    	// size of q: 3
    	cout << "front element: " << q.front() << endl;
    	// front element: 1
    
    	while (!q.empty()) {
    		cout << q.front() << ' ';
    		q.pop();
    	}
    	// 1 3 5
    	cout << endl;
    }
    
    class Teacher
    {
    public:
    	int age;
    	char name[32];
    public:
    	void printTeacher()
    	{
    		cout << "age: " << age << endl;
    	}
    };
    
    void queueClass()
    {
    	Teacher t1, t2, t3;
    	t1.age = 21;
    	t2.age = 22;
    	t3.age = 23;
    
    	queue<Teacher> q1;
    	q1.push(t1);
    	q1.push(t2);
    	q1.push(t3);
    	while (!q1.empty()) {
    		Teacher tmp = q1.front();
    		q1.pop();
    		tmp.printTeacher();
    	}
    	cout << endl;
    	/*
    	age: 21
    	age: 22
    	age: 23
    	*/
    
    	queue<Teacher *> q2;
    	q2.push(&t1);
    	q2.push(&t2);
    	q2.push(&t3);
    	while (!q2.empty()) {
    		Teacher *tmp = q2.front();
    		q2.pop();
    		tmp->printTeacher();
    	}
    	cout << endl;
    	/*
    	age: 21
    	age: 22
    	age: 23
    	*/
    
    }
    
    int main()
    {
    	queueInit();
    	queueClass();
    
    	return 0;
    }
  • 相关阅读:
    .NET反编译工具:de4dot
    Hadoop API:遍历文件分区目录,并根据目录下的数据进行并行提交spark任务
    hive优化之——控制hive任务中的map数和reduce数
    Spark:将RDD[List[String,List[Person]]]中的List[Person]通过spark api保存为hdfs文件时一直出现not serializable task,没办法找到"spark自定义Kryo序列化输入输出API"
    spark算子:combineByKey
    spark分区数,task数目,core数,worker节点个数,excutor数量梳理
    spark算子:partitionBy对数据进行分区
    算子:sample(false, 0.1)抽样数据
    hive:默认允许动态分区个数为100,超出抛出异常:
    Oracle ADDM性能诊断利器及报告解读
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6740907.html
Copyright © 2011-2022 走看看