[英]What are differences between deque and linked list (+vector) implementation for STL queue (OR stack)?
[英]Converting between STL stack and deque
有沒有一種方法可以將C ++ STL雙端隊列轉換為堆棧,反之亦然,而無需手動遍歷源代碼的每個元素? (我不確定<cast>
之一在這里是否可用。)
stack
只是一個容器適配器,因此您可以將deque
對象傳遞給它以將其用作容器:
std::deque<int> my_deque;
// Do something with deque here
std::stack<int> s(my_deque);
要隱蔽,可以將構造函數與迭代器一起使用:
我不確定是否可以進行直接轉換(除了直接使用my_deque
)。 我唯一能想到的是:
std::deque<int> other_deck;
// Construct new stack:
std::stack<int> new_stack(other_deck);
// use std::swap
std::swap(new_stack, old_stack);
現在other_deck
應該從充滿了old_stack
。
與其他適配器交換容器適配器的內容。
using std::swap; swap(c, other.c);
有效調用using std::swap; swap(c, other.c);
using std::swap; swap(c, other.c);
編輯似乎swap
只是交換基礎容器,而不交換容器的內容,因此這將不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.