stl容器deque_C ++ STL双端队列容器– std :: deque

àì夳堔傛蜴生んèń 2022-12-07 13:55 322阅读 0赞

8b3926a5776d63ff97b44b8c4bdd6dbf.png

stl容器deque

Here you will learn about C++ STL Deque container i.e. std::deque and all functions applicable on it.

在这里,您将了解C ++ STL Deque容器,即std :: deque及其适用的所有功能。

Note: Deque should be pronounced as “deck”.

注意:双端队列应发音为“ deck”。

It named because Double Ended Queue (DEQUE). Deques are come under sequence containers. These are double ended with features of expansion and contraction on both the ends. These are similar to vectors. But more efficient than vectors in case of insertion and deletion of elements not only at end but also at the beginning of the sequence. But here contiguous memory allocation may not be guaranteed.

它命名,因为d oubleËnded UE(双端队列)。 双端队列位于序列容器下。 这些都是双头的,两端都有膨胀和收缩的功能。 这些类似于矢量。 但是,不仅在序列的结尾而且在序列的开头插入和删除元素时,其效率都比载体高。 但是,此处可能无法保证连续的内存分配。

Also Read: C++ STL Vector Container – std::vector

另请阅读: C ++ STL矢量容器– std :: vector

C ++ STL双端队列 (C++ STL Deque)

To use deque we must include its header i.e. #include

要使用双端队列,我们​​必须包含其头文件#include

声明双端队列的不同语法 (Different Syntax for Declaring Deque)

Creating an empty deque:

创建一个空的双端队列:

  1. deque <int> dequeName;

Creating a deque with 10 empty elements:

创建具有10个空元素的双端队列:

  1. deque <int> marks(10);

Creating a deque with 10 elements, each element have value 3:

创建一个包含10个元素的双端队列,每个元素的值为3:

  1. deque <int> marks(10,3);

Array to deque:

数组进行双端队列处理:

  1. int array [5] = {1, 2, 3, 4, 5};
  2. deque <int> ranks(array,array+5);

Copying all deque elements to another deque:

将所有双端队列元素复制到另一个双端队列:

  1. #include<iostream>
  2. #include<deque>
  3. using namespace std;
  4. int main(){
  5. deque <int> deq1(5,10);
  6. deque <int> deq2(deq1);
  7. for(int i=0; i<5; i++)
  8. cout << deq1[i] << " " ;
  9. cout << endl;
  10. for(int i=0; i<5; i++)
  11. cout << deq2[i] << " " ;
  12. return 0;
  13. }

Output

输出量

10 10 10 10 10 10 10 10 10 10

10 10 10 10 10 10 10 10 10 10

将元素插入双端队列 (Inserting Elements Into Deque)

push_back(element): This inserts the element at the end of the deque.

push_back(element):会将元素插入双端队列的末尾。

push_front(element): This function inserts the element at the front of the deque.

push_front(element):此函数将元素插入双端队列的前面。

insert(): insert() function can be used in different ways.

insert(): insert()函数可以以不同的方式使用。

  • We can insert an element at particular position pointed by the iterator. For this we use two arguments. Those are (iterator, value to be inserted) respectively.

    我们可以在迭代器指向的特定位置插入元素。 为此,我们使用两个参数。 它们分别是(迭代器,要插入的值)。

  • We can insert an element, “n” no. of times at front of the deque. For this we use three arguments. Those are (iterator, number n, inserted value) respectively.

    我们可以插入一个元素“ n”。 在双端队列的前面。 为此,我们使用三个参数。 它们分别是(迭代器,编号n,插入值)。

  • We can insert array elements form particular index to another index. For this we use three arguments, (iterator, arrayStartIndex, arrayLastIndex);

    我们可以将数组元素从特定索引插入到另一个索引。 为此,我们使用三个参数(迭代器,arrayStartIndex,arrayLastIndex);

assign(): assign (num, value), this inserts value into deque num times.

Assign(): assign(数字,值),这会将值插入双端队列num次。

Example program to show different ways of inserting element into deque:

示例程序展示了将元素插入双端队列的不同方式:

  1. #include<iostream>
  2. #include<deque>
  3. using namespace std;
  4. int main(){
  5. int element;
  6. deque <int> dek;
  7. cout << "enter an element to insert at back" << endl;
  8. cin >> element;
  9. dek.push_back (element);
  10. cout << "enter an element to insert at front" << endl;
  11. cin >> element;
  12. dek.push_front (element);
  13. deque <int> :: iterator it;
  14. it = dek.begin();
  15. // inserting at particluar posiion using insert()
  16. cout << "inserting element 15 at start of the deque using iterator" << endl;
  17. dek.insert(it, 15);
  18. // inserting element, 2 times at the end of the deque
  19. cout << "inserting element 10, two times at end of the deque" << endl;
  20. it = dek.end();
  21. dek.insert (it, 2, 10);
  22. // inserting first 3 elements of the array to front of the deque
  23. cout << "Inserting first 3 elemtns of the array(1,2,3) to at the fornt of the deque" << endl;
  24. it = dek.begin();
  25. int array[5] = { 1, 2, 3, 4, 5};
  26. dek.insert(it, array, array+3);
  27. cout << "firnal result is" << endl;
  28. for(int i=0; i<dek.size(); i++)
  29. cout << dek[i] << " ";
  30. // using assign() to insert elements
  31. cout << "using assign inserting into new deque" << endl;
  32. deque <int> newdq;
  33. newdq.assign(5,99);
  34. cout << "New deque elements are" << endl;
  35. for(int i=0; i<newdq.size(); i++)
  36. cout << newdq[i] << " ";
  37. return 0;
  38. }

Output

输出量

enter an element to insert at back 10 enter an element to insert at front 15 inserting element 15 at start of the deque using iterator inserting element 10, two times at end of the deque Inserting first 3 elemtns of the array(1,2,3) to at the fornt of the deque firnal result is 1 2 3 15 15 10 10 10 using assign inserting into new deque New deque elements are 99 99 99 99 99

输入元件在靠背 10 以插入 输入的元件 用iterator 在双端队列 插入第一3 elemtns阵列的 端部的插入元件10,两次 在前面 15 在双端队列的开始插入元件15 插入 (1,2,3 )至双端队列的 最终结果是 使用分配插入新双 端队列的 1 2 3 15 15 10 10 10 10新双 端队列元素为 99 99 99 99 99

删除元素形成双端队列 (Deleting Elements form Deque)

pop_back(): This will deletes the last element of the deque.

pop_back():这将删除双端队列的最后一个元素。

pop_front(): This will deletes the first element of the deque.

pop_front():这将删除双端队列的第一个元素。

erase():** **This function deletes the element which pointed by the iterator at particular position.

擦除(): 此函数删除迭代器指向特定位置的元素。

clear(): This function removes all elements from the deque.

clear():此函数从双端队列中删除所有元素。

Example program to show different ways of deleting an element form deque:

示例程序展示了删除元素形式双端队列的不同方法:

  1. #include<iostream>
  2. #include<deque>
  3. using namespace std;
  4. int main(){
  5. deque <int> dek;
  6. for(int i=0; i<7; i++)
  7. dek.push_back(i);
  8. cout << "Initially deque contains elements are " << endl;
  9. for(int i=0; i<7; i++)
  10. cout << dek[i] << " ";
  11. cout << endl;
  12. // uisng pop_back
  13. cout << "Deleting last element using pop_back" << endl;
  14. dek.pop_back();
  15. // using pop_front
  16. cout << "Deleting first element using pop_fornt" << endl;
  17. dek.pop_front();
  18. // deleting an element at particular position
  19. deque <int> :: iterator it;
  20. it = dek.begin();
  21. cout << "deleting element at index 2" << endl;
  22. dek.erase(it+2);
  23. cout << "Resultant deque upto now-> " << endl;
  24. for(int i=0; i<dek.size(); i++)
  25. cout << dek[i] << " ";
  26. cout << endl;
  27. // clear functios.
  28. cout << "using clear function" << endl;
  29. dek.clear();
  30. // checking dek is empty or not.
  31. dek.empty() ? cout << "finally deque is empty" : cout << "deque is not empty";
  32. return 0;
  33. }

Output

输出量

Initially deque contains elements are 0 1 2 3 4 5 6 Deleting last element using pop_back Deleting first element using pop_fornt deleting elements at index 2 Resultant deque upto now-> 1 2 4 5 using clear function finally deque is empty

最初的deque包含元素为 0 1 2 3 4 5 6 使用pop_back删除最后 一个元素使用pop_fornt 删除索引为2的元素 删除第一个元素 结果 当前的deque- > 1 2 4 5 使用clear函数 最后deque为空

resize(): Resize can be applied for increasing or decreasing the current size of the deque.

resize():调整大小可用于增加或减少双端队列的当前大小。

size(): Returns an integer that the number of elements present in the deque

size():返回一个整数,该整数表示双端队列中存在的元素数

Max_size(): Returns a system and architecture dependent value.

Max_size():返回与系统和体系结构相关的值。

empty(): This is Boolean function, that returns true if deque is empty returns false if it’s not empty.

empty():这是布尔函数,如果双端队列为空,则返回true;如果不为空,则返回false。

swap(): It swaps the all elements of deque1 to deque2. And all values of deque2 to deque1.

swap():将deque1的所有元素交换为deque2。 并将deque2到deque1的所有值。

Example program to demonstrate above functions:

演示上述功能的示例程序:

  1. #include<iostream>
  2. #include<deque>
  3. using namespace std;
  4. int main(){
  5. deque <int> dek;
  6. for(int i=0; i<5; i++)
  7. dek.push_back(i);
  8. cout << "deque size is " << dek.size() << endl;
  9. // resizing
  10. dek.resize(3);
  11. cout << "deque size after resize is " << dek.size() << endl;
  12. cout << "maxsize of deque is " << dek.max_size() << endl;
  13. //checking empty condition
  14. dek.empty() ? cout << "finally deque is empty" : cout << "deque is not empty";
  15. cout << endl;
  16. deque <int> d1(5,10);
  17. deque <int> d2(5,20);
  18. cout << "Elements of the deque1 before swap" << endl;
  19. for(int i=0; i<5; i++)
  20. cout << d1[i] << " ";
  21. cout << endl;
  22. cout << "Elements of the deque2 before swap" << endl;
  23. for(int i=0; i<5; i++)
  24. cout << d2[i] << " ";
  25. cout << endl;
  26. cout << "Elements of the deque1 after swap" << endl;
  27. d1.swap(d2);
  28. for(int i=0; i<5; i++)
  29. cout << d1[i] << " ";
  30. cout << endl;
  31. cout << "Elements of the deque2 after swap" << endl;
  32. for(int i=0; i<5; i++)
  33. cout << d2[i] << " ";
  34. cout << endl;
  35. return 0;
  36. }

Output

输出量

deque size is 5 deque size after resize is 3 maxsize of deque is 4611686018427387903 deque is not empty Elements of the deque1 before swap 10 10 10 10 10 Elements of the deque2 before swap 20 20 20 20 20 Elements of the deque1 after swap 20 20 20 20 20 Elements of the deque2 after swap 10 10 10 10 10

deque大小为5 调整大小后为3的deque大小 deque的最大值为4611686018427387903 deque不为空 交换之前的deque1元素 10 10 10 10 10 交换之前的deque2元素 20 20 20 20 20 交换之后的deque1元素 20 20 20 20 20 交换后的双端队列 2的 元素 10 10 10 10 10

Comment below if you have queries or found any information incorrect in above tutorial for C++ STL Deque.

如果您有疑问或在上面的C ++ STL Deque教程中发现任何不正确的信息,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/07/stl-deque.html

stl容器deque

发表评论

表情:
评论列表 (有 0 条评论,322人围观)

还没有评论,来说两句吧...

相关阅读

    相关 STL(四) deque 队列

    deque 底层数据结构 vector是单向开口的线性连续空间,deque则是一种双向开口的连续数据空间。所谓的双向开口,意思是可以在头尾两端分别做元素的插入和删除操作。当