zoukankan      html  css  js  c++  java
  • C++ queue用法

    C++ queue用法

    只能访问queue容器适配器的第一个和最后一个元素。只能在容器的末尾添加新元素,只能从头部移除元素。

    queue操作

    • front():返回queue中第一个元素的引用。如果queue是常量,就返回一个常引用,如果queue为空,返回值是未定义的。
    • back():返回queue中最后一个元素引用
    • push(const T &obj):在queue的尾部添加一个元素的副本。
    • pop():删除queue中的第一个元素
    • size():返回queue中元素的个数
    • empty():如果queue中没有元素返回true。
    • emplace():用传给emplace()的参数调用T的构造函数,在queue的尾部生成对象。

    例子:

    #include<Windows.h>
    #include <iostream>
    #include<queue>
    #include<string>
    #include<thread>
    #include<mutex>
    using namespace std;
    
    ::queue<int> q;
    ::mutex mutex1;
    void fun1()
    {
    	while (true)
    	{
    		lock_guard<mutex> guard(mutex1);
    		q.push(clock());//入队列
    		std::this_thread::sleep_for(1ms);
    	}
    }
    
    void fun2()
    {
    	while (true)
    	{
    		lock_guard<mutex> guard(mutex1);
    		if (!q.empty())//先判断队列是否有元素
    		{
    			cout << q.front() << endl;//获取第一个元素的值
    			q.pop();//弹出第一个元素
    		}
    		std::this_thread::sleep_for(2ms);
    	}
    }
    
    int main()
    {
    	thread th1(fun1);
    	thread th2(fun2);
    	th1.detach();
    	th2.detach();
    	int c = ::getchar();
    }
    
  • 相关阅读:
    Linux Shell tr 命令详解
    Shell统计每个单词出现的个数
    启明4-29团队进度博客
    启明4-28团队进度博客
    启明4-27团队进度博客
    启明4-26团队进度博客
    启明4-25团队进度博客
    启明4-24团队博客
    每日总结8
    启明4-23团队进度博客
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/14435610.html
Copyright © 2011-2022 走看看