C++ की deque
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Deque में Elements Access करना
क्योंकि एक deque random access support करता है, आप d[i] जैसे index brackets से, या bounds-checked at() method से सीधे किसी भी element तक पहुँच सकते हैं, exactly उसी तरह जैसे आप एक vector के साथ करते।
उदाहरण: Accessing Elements in Deque
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक deque के अंत से आगे
[]इस्तेमाल करना, जो undefined behavior है। - यह मान लेना कि एक deque elements को एक vector की तरह एक contiguous block में store करता है।
- एक loop में elements erase करना और एक invalidated iterator से जारी रखना।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: