Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน deque::back() ใน C++ คืออะไร?


ฟังก์ชัน deque::back() ใช้สำหรับอ้างอิงองค์ประกอบสุดท้ายของ deque

ไวยากรณ์

deque_name.back()

ตัวอย่าง

อินพุต Deque - 11 12 13 14 15

เอาต์พุต New Deque - 15

อินพุต Deque - C H O I C E

เอาต์พุต New Deque - E

แนวทางการทำงาน

  • ประกาศ deque ก่อน
  • พิมพ์ deque
  • กำหนดฟังก์ชัน back()

ด้วยแนวทางด้านบน เราสามารถดึงองค์ประกอบสุดท้ายของ deque ได้

ตัวอย่าง

// C++ code to demonstrate the working of deque back() function
#include <iostream>
#include <deque>
using namespace std;

int main() {
    // initializing deque
    deque<int> dq = { 14, 15, 16, 17, 18 };
    cout << "Deque: ";
    for(auto x = dq.begin(); x != dq.end(); ++x)
        cout << *x << " ";
    // defining the back() function
    cout << "\nBack element: " << dq.back();
    return 0;
}

เอาต์พุต

Deque: 14 15 16 17 18
Back element: 18