ไลบรารีแม่แบบมาตรฐานของภาษาซีพลัสพลัส/queue

จาก testwiki
รุ่นแก้ไขเมื่อ 17:32, 23 มีนาคม 2567 โดย imported>Minorax
(ต่าง) ←รุ่นแก้ไขก่อนหน้า | รุ่นแก้ไขล่าสุด (ต่าง) | รุ่นแก้ไขถัดไป→ (ต่าง)
ไปยังการนำทาง ไปยังการค้นหา

queue เป็นโครงสร้างข้อมูล แบบ FIFO


การใช้งานและการประกาศตัวแปร

ต้องนำเข้า header file "queue" โดย #include <queue>

ให้ T คือชนิดข้อมูลใดๆ และ var คือชื่อตัวแปร มีรูปแบบการประกาศตัวแปร queue โดย queue <T> var;

method

push

แม่แบบ:STLdata

pop

แม่แบบ:STLdata

front

แม่แบบ:STLdata

size

แม่แบบ:STLdata

empty

แม่แบบ:STLdata

ตัวอย่างโค้ด

#include <cstdio>
#include <queue>

using namespace std;

struct ST{
    int a, b;
};

int main(){
    queue <int> Q;               // []
    Q.push(13);                  // [13]
    Q.push(12);                  // [13,12]
    Q.push(11);                  // [13,12,11]
    Q.push(10);                  // [13,12,11,10]
    printf("%d\n", Q.front());   // => 13
    Q.pop();                     // [12,11,10]
    printf("%d\n", Q.size());    // => 3
    while(not Q.empty()) Q.pop();    // []
    Q.pop()                      // => Error
    Q.top()                      // => Error
    
    ST tmp;
    queue <ST> T;                // []
    tmp.a = 5;
    tmp.b = 3;
    T.push(tmp);                 // [(5,3)]
    printf("%d\n", T.front().b); // => 3
    return 0;
}

สารบัญ

แม่แบบ:Stage short stack
แม่แบบ:Stage short queue
แม่แบบ:Stage short priority_queue
แม่แบบ:Stage short deque
แม่แบบ:Stage short vector
แม่แบบ:Stage short map
แม่แบบ:Stage short set