A deque (double-ended queue) is a data structure that allows insertion and deletion of elements from both ends. It is part of the C++ Standard Library and provides a flexible array with efficient access to both ends.
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq;
dq.push_back(1);
dq.push_front(2);
for(int n : dq)
cout << n << ' ';
return 0;
}
Console Output:
2 1
Deques provide several ways to access elements:
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq = {10, 20, 30};
cout << dq.at(1) << ' ' << dq[2] << ' ' << dq.front() << ' ' << dq.back();
return 0;
}
Console Output:
20 30 10 30
Deques allow modifications through various methods:
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq = {1, 2, 3};
dq.insert(dq.begin() + 1, 4);
dq.erase(dq.begin());
for(int n : dq)
cout << n << ' ';
return 0;
}
Console Output:
4 2 3
Deques manage their size dynamically:
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq = {1, 2, 3};
cout << "Size: " << dq.size() << endl;
dq.resize(5);
cout << "New Size: " << dq.size();
return 0;
}
Console Output:
Size: 3 New Size: 5
Deques can be iterated using iterators:
#include <iostream>
#include <deque>
using namespace std;
int main() {
deque<int> dq = {1, 2, 3};
for(auto it = dq.begin(); it != dq.end(); ++it)
cout << *it << ' ';
return 0;
}
Console Output:
1 2 3
Both deques and vectors provide dynamic arrays, but they have differences:
#include <iostream>
#include <deque>
#include <vector>
using namespace std;
int main() {
deque<int> dq = {1, 2, 3};
vector<int> vec = {1, 2, 3};
dq.push_front(0);
vec.push_back(4);
cout << "Deque Front: " << dq.front() << ", Vector Back: " << vec.back();
return 0;
}
Console Output:
Deque Front: 0, Vector Back: 4
Deques are versatile and can be used in various scenarios:
#include <iostream>
#include <deque>
using namespace std;
bool isPalindrome(deque<char>& dq) {
while(dq.size() > 1) {
if(dq.front() != dq.back())
return false;
dq.pop_front();
dq.pop_back();
}
return true;
}
int main() {
deque<char> dq = {'r', 'a', 'c', 'e', 'c', 'a', 'r'};
cout << (isPalindrome(dq) ? "Palindrome" : "Not Palindrome");
return 0;
}
Console Output:
Palindrome
Understanding the performance characteristics of deques is crucial for efficient programming:
#include <iostream>
#include <deque>
#include <ctime>
using namespace std;
int main() {
clock_t start, end;
deque<int> dq;
start = clock();
for(int i = 0; i < 1000000; ++i)
dq.push_back(i);
end = clock();
cout << "Time taken: " << double(end - start) / CLOCKS_PER_SEC << " seconds";
return 0;
}
Console Output:
Time taken: X.XXXX seconds
Utilizing deques in advanced scenarios can optimize solutions:
#include <iostream>
#include <deque>
#include <mutex>
using namespace std;
mutex mtx;
void threadSafePush(deque<int>& dq, int val) {
lock_guard<mutex> lock(mtx);
dq.push_back(val);
}
int main() {
deque<int> dq;
threadSafePush(dq, 10);
cout << dq.front();
return 0;
}
Console Output:
10
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies