forward-list

Why is splicing an entire list or a range linear for std::forward_list?

本秂侑毒 提交于 2020-01-12 07:18:29
问题 Splicing a range from one list to another can be accomplished in constant time, at the expense of making size() 's complexity linear. C++11 has changed that in case of std::list by requiring size() to be constant time. This broke, for example, gcc's implementation, see [C++0x] std::list::size complexity. Apart from the range splice() , is there any other reason why size() could not be made constant time in the earlier, C++03 conforming std::list implementations? Why is splicing an entire list

Why is splicing an entire list or a range linear for std::forward_list?

时光怂恿深爱的人放手 提交于 2020-01-12 07:18:19
问题 Splicing a range from one list to another can be accomplished in constant time, at the expense of making size() 's complexity linear. C++11 has changed that in case of std::list by requiring size() to be constant time. This broke, for example, gcc's implementation, see [C++0x] std::list::size complexity. Apart from the range splice() , is there any other reason why size() could not be made constant time in the earlier, C++03 conforming std::list implementations? Why is splicing an entire list

std::forward_list — erasing with a stored iterator

蹲街弑〆低调 提交于 2019-12-20 02:45:30
问题 I'm trying to keep a global list of a particular (base) class's instances so that I can track them down by iterating through this global list at any time. I believe the most proper way to address this is with an intrusive list. I have heard that one can encounter these creatures by digging into the Linux kernel, for example. In the situation where I'm in, I don't really need such guarantees of performance, and using intrusive lists will complicate matters somewhat for me. Here's what I've got

How to do range splice in constant time with std::forward_list?

风格不统一 提交于 2019-12-19 19:00:50
问题 I want to splice the range [first, last] , with both endpoints inclusive. I have iterators to the element before first and to last . I could do it with splice_after() but only in linear time. I belive this splice can be done in constant time. How can I do it with std::forward_list ? If the question is not clear, here as is an example code showing my problem: Code on Live Work Space #include <algorithm> #include <forward_list> #include <iostream> #include <iterator> using namespace std; int

When to use C++ forward_list

北城以北 提交于 2019-12-04 17:52:11
问题 I am kind of new to C++, and reading the book "The C++ Programming Language (4th edition)". When reading chapter of "STL Containers", the book has a introduction to forward_list: A forward_list (a singly-linked list) is basically a list optimized for empty and very short lists. An empty forward_list takes up only one word. There are surprisingly many uses for lists where most are empty (and the rest are very short). I am wondering how short a list is considering short? And could anyone give a

Why is splicing an entire list or a range linear for std::forward_list?

梦想的初衷 提交于 2019-12-04 04:00:30
Splicing a range from one list to another can be accomplished in constant time, at the expense of making size() 's complexity linear. C++11 has changed that in case of std::list by requiring size() to be constant time. This broke, for example, gcc's implementation, see [C++0x] std::list::size complexity . Apart from the range splice() , is there any other reason why size() could not be made constant time in the earlier, C++03 conforming std::list implementations? Why is splicing an entire list or a range linear for std::forward_list ? See splice_after() , cases (1) and (3). See also 23.3.4.6

How do I efficiently remove_if only a single element from a forward_list?

五迷三道 提交于 2019-12-04 03:12:44
问题 Well I think the question pretty much sums it up. I have a forward_list of unique items, and want to remove a single item from it: std::forward_list<T> mylist; // fill with stuff mylist.remove_if([](T const& value) { return value == condition; }); I mean, this method works fine but it's inefficient because it continues to search once the item is found and deleted. Is there a better way or do I need to do it manually? 回答1: If you only want to remove the first match, you can use std::adjacent

forward_list: assign(_InputIterator __first, _InputIterator __last) / assign(size_type __n, const _Tp& __val)

痞子三分冷 提交于 2019-12-02 11:43:26
问题 I have implemented a subset of the forward_list and wanted to test the method assign(size_type __n, const _Tp& __val) but I get a compiler error because the compiler wants to call the method assign(_InputIterator __first, _InputIterator __last) instead. I have written the following snippet, just to illustrate the problem: test.h #ifndef TEST_H #define TEST_H #include <utility> // Just to get the std::size_t template<typename _Tp> class forward_list { public: typedef std::size_t size_type;

How to do range splice in constant time with std::forward_list?

删除回忆录丶 提交于 2019-12-01 17:50:22
I want to splice the range [first, last] , with both endpoints inclusive. I have iterators to the element before first and to last . I could do it with splice_after() but only in linear time. I belive this splice can be done in constant time. How can I do it with std::forward_list ? If the question is not clear, here as is an example code showing my problem: Code on Live Work Space #include <algorithm> #include <forward_list> #include <iostream> #include <iterator> using namespace std; int main() { forward_list<char> trg{'a','b','c'}; forward_list<char> src{'1','2','3','4'}; auto before_first

How do I efficiently remove_if only a single element from a forward_list?

血红的双手。 提交于 2019-12-01 16:34:30
Well I think the question pretty much sums it up. I have a forward_list of unique items, and want to remove a single item from it: std::forward_list<T> mylist; // fill with stuff mylist.remove_if([](T const& value) { return value == condition; }); I mean, this method works fine but it's inefficient because it continues to search once the item is found and deleted. Is there a better way or do I need to do it manually? If you only want to remove the first match, you can use std::adjacent_find followed by the member erase_after #include <algorithm> #include <cassert> #include <forward_list>