基礎
# include <queue>
# include <iostream>
void Test01 ( ) { std:: queue< int > q; q. push ( 1 ) ; q. push ( 2 ) ; q. push ( 3 ) ; q. push ( 4 ) ; q. push ( 777 ) ; std:: cout << "隊列大小:" << q. size ( ) << std:: endl; std:: cout << "隊頭元素:" << q. front ( ) << std:: endl; std:: cout << "隊尾元素:" << q. back ( ) << std:: endl;
}
void Test02 ( ) { std:: queue< int > q; q. push ( 1 ) ; q. push ( 2 ) ; q. push ( 3 ) ; q. push ( 4 ) ; q. push ( 777 ) ; std:: cout << "隊列大小:" << q. size ( ) << std:: endl; while ( ! q. empty ( ) ) { std:: cout << q. front ( ) << std:: endl; q. pop ( ) ; } std:: cout << "隊列大小:" << q. size ( ) << std:: endl; } int main ( ) { Test02 ( ) ; return 0 ;
}
隊列的特點是先進先出。 隊列是種邏輯結構,和數組vector不一樣,vector是物理結構。 隊列可以用數組實現,也可以用鏈表實現。 隊列的思想是生產者消費者模式 。往隊列里push就是生產,出隊pop操作就是消費。 現實中的隊列,比如說快遞站。快遞員往快遞站放快遞就是生產,顧客取快遞就是消費。 app中的隊列。雙十一,淘寶用戶的訂單會先push到隊列,然后慢慢消費隊列,追蹤每個訂單的狀態。