deque

How to slice a deque? [duplicate]

被刻印的时光 ゝ 提交于 2019-11-30 10:55:21
This question already has an answer here: Use slice notation with collections.deque 6 answers I've changed some code that used a list to using a deque. I can no longer slice into it, as I get the error: TypeError: sequence index must be integer, not 'slice' Here's a REPL that shows the problem. >>> import collections >>> d = collections.deque() >>> for i in range(3): ... d.append(i) ... >>> d deque([0, 1, 2]) >>> d[2:] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: sequence index must be integer, not 'slice' So, is there a workaround to support slicing into

Why is it so slow iterating over a big std::list?

南楼画角 提交于 2019-11-30 10:37:05
As title suggests, I had problems with a program of mine where I used a std::list as a stack and also to iterate over all elements of the list. The program was taking way too long when the lists became very big. Does anyone have a good explanation for this? Is it some stack/cache behavior? (Solved the problem by changing the lists to std::vector and std::deque (an amazing data structure by the way) and everything suddenly went so much faster) EDIT: I'm not a fool and I don't access elements in the middle of the lists. The only thing I did with the lists was to remove/add elements at the end

Converting a deque object into list

我的未来我决定 提交于 2019-11-30 05:43:49
currently I fetch "list" data from my storage, "deque" it to work with that data. After processing the fetched data I have to put them back into the storage. This won't be a problem as long as I am not forced (at least I think so) to use python's standard "list" object to save this data. Storage Service: Google Appengine. My work-around would be: dequeObj = deque(myData) my_list= list() for obj in dequeObj: my_list.append(obj) but this seems not very optimal. >>> list(collections.deque((1, 2, 3))) [1, 2, 3] 来源: https://stackoverflow.com/questions/5773397/converting-a-deque-object-into-list

“move” two vectors together

寵の児 提交于 2019-11-29 21:33:05
If I have two vectors and want to combine them to one, I can do it the following way: std::vector<T> a(100); // just some random size here std::vector<T> b(100); a.insert(std::end(a), std::begin(b), std::end(b)); That involves copying though, which I want to avoid. Is there any way to use move-semantics to get them together? I highly doubt it, as a vector is supposed to be contiguous. However is there any way to do it with a deque ? Yes, use std::move : #include <algorithm> std::move(b.begin(), b.end(), std::back_inserter(a)); Alternatively, you can use move iterators: a.insert(a.end(), std:

About deque<T>'s extra indirection

混江龙づ霸主 提交于 2019-11-29 07:21:08
Wondering why my memory accesses were somewhat slower than I expected, I finally figured out that the Visual C++ implementation of deque indeed has an extra layer of indirection built-in, destroying my memory locality. i.e. it seems to hold an array of T* , not an array of T . Is there another implementation I can use with VC++ that doesn't have this "feature", or is there some way (although I consider it unlikely) to be able to avoid it in this implementation? I'm basically looking for a vector that has also O(1) push/pop at the front. I guess I could implement it myself, but dealing with

Converting a deque object into list

穿精又带淫゛_ 提交于 2019-11-29 04:52:59
问题 currently I fetch "list" data from my storage, "deque" it to work with that data. After processing the fetched data I have to put them back into the storage. This won't be a problem as long as I am not forced (at least I think so) to use python's standard "list" object to save this data. Storage Service: Google Appengine. My work-around would be: dequeObj = deque(myData) my_list= list() for obj in dequeObj: my_list.append(obj) but this seems not very optimal. 回答1: >>> list(collections.deque(

Confusion on iterators invalidation in deque

*爱你&永不变心* 提交于 2019-11-29 03:35:58
I'm bit confused regarding iterator invalidation in deque. (In the context of this question) Following is the excerpts from -- The C++ Standard Library: A Tutorial and Reference, By Nicolai M. Josuttis Any insertion or deletion of elements other than at the beginning or end invalidates all pointers, references, and iterators that refer to elements of the deque. Following is the excerpts from SGI site: The semantics of iterator invalidation for deque is as follows. Insert (including push_front and push_back ) invalidates all iterators that refer to a deque. Erase in the middle of a deque

Time complexity of removing items in vectors and deque

烂漫一生 提交于 2019-11-29 01:34:59
I have read that time complexity of adding items to end of a std::vector is amortized constant and inserting items at the top and bottom of a std::deque is constant.Since both these containers have a random access iterator thus accessing elements at any index is constant. Please let me know if I have any of these facts wrong.My question is if accessing an element in a std::vector or std::deque is constant then why is the time complexity of removing an element via erase O(n). One of the answers here here states that removing elements via erase is O(n). I know that erase removes the elements

Why does push_back or push_front invalidate a deque's iterators?

廉价感情. 提交于 2019-11-29 01:06:59
As the title asks. My understanding of a deque was that it allocated "blocks". I don't see how allocating more space invalidates iterators, and if anything, one would think that a deque's iterators would have more guarantees than a vector's, not less. The C++ standard doesn't specify how deque is implemented. It isn't required to allocate new space by allocating a new chunk and chaining it on to the previous ones, all that's required is that insertion at each end be amortized constant time. So, while it's easy to see how to implement deque such that it gives the guarantee you want[*], that's

“move” two vectors together

老子叫甜甜 提交于 2019-11-28 17:11:41
问题 If I have two vectors and want to combine them to one, I can do it the following way: std::vector<T> a(100); // just some random size here std::vector<T> b(100); a.insert(std::end(a), std::begin(b), std::end(b)); That involves copying though, which I want to avoid. Is there any way to use move-semantics to get them together? I highly doubt it, as a vector is supposed to be contiguous. However is there any way to do it with a deque ? 回答1: Yes, use std::move : #include <algorithm> std::move(b