← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 4 of 15

C++ की deque

एक deque एक list है जहाँ आप दोनों ends पर जल्दी items add या remove कर सकते हैं, एक line की तरह जहाँ लोग front या back से join या leave कर सकते हैं। यह double-ended queue का short है।
Syntax
cpp
#include <deque>

std::deque<data_type> deque_name = {value1, value2};
deque_name.push_front(value);
deque_name.push_back(value);

deque क्या है?

एक deque, double-ended queue का short, एक sequence container है जो एक vector और एक linked list के hybrid जैसा behave करता है: यह दोनों ends पर fast insertion और removal offer करता है जबकि अभी भी vector की तरह direct random-access indexing support करता है।

उदाहरण: What is a deque?

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {1, 2, 3};
	// Print to the console with cout
	std::cout << d[0] << " " << d.front() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Front और Back पर Fast Inserts

एक vector के उलट, एक deque push_front() इस्तेमाल करके constant time में अपने front में elements add करना support करता है, आपकी पहले से उम्मीद की push_back() के अलावा — एक capability जो vectors बस efficiently offer नहीं करते।

उदाहरण: Fast Inserts at Front and Back

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {2, 3};
	d.push_front(1);
	d.push_back(4);
	for (int x : d) std::cout << x << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Deque में Elements Access करना

क्योंकि एक deque random access support करता है, आप d[i] जैसे index brackets से, या bounds-checked at() method से सीधे किसी भी element तक पहुँच सकते हैं, exactly उसी तरह जैसे आप एक vector के साथ करते।

उदाहरण: Accessing Elements in Deque

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {10, 20, 30};
	// Print to the console with cout
	std::cout << d[1] << " " << d.at(2) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Deque से Elements Remove करना

आप pop_front() या pop_back() इस्तेमाल करके एक deque के किसी भी end से elements remove कर सकते हैं, और दोनों operations automatically container की reported size update करते हैं, विपरीत end पर push और pop कैसे काम करते हैं यह mirror करते हुए।

उदाहरण: Removing Elements from Deque

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {1, 2, 3};
	d.pop_front();
	d.pop_back();
	// Print to the console with cout
	std::cout << d.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Deque पर Iterate करना

एक vector की तरह ही, एक deque को एक index-based loop, एक range-based for loop, या STL iterators से traverse किया जा सकता है — uniform iteration interface STL के shared container conventions पर build करने के main benefits में से एक है।

उदाहरण: Iterating over Deque

cpp
// Include std::cout and std::cin
#include <iostream>
#include <deque>

// Program execution starts in main()
int main() {
	std::deque<int> d = {1, 2, 3};
	// Loop: repeat while the condition holds
	for (int i = 0; i < d.size(); i++) std::cout << d[i] << " ";
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक deque के अंत से आगे [] इस्तेमाल करना, जो undefined behavior है।
  2. यह मान लेना कि एक deque elements को एक vector की तरह एक contiguous block में store करता है।
  3. एक loop में elements erase करना और एक invalidated iterator से जारी रखना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.