stdlist

How does std::list allocate nodes vs. elements

折月煮酒 提交于 2019-12-10 03:21:26
问题 How does std::list allocate the nodes in which it keeps the next / prev pointers and the T element it contains? I think that standard allocators can only be used to allocate memory for one type (because std::allocator::allocate allocates memory in increments of sizeof(T) ). So it seems impossible to allocate the list node and the contained object in a single allocation, which means that the nodes have to be allocated with whatever the implementation decides, and the nodes store pointers to

std::list<std::future> destructor does not block

╄→гoц情女王★ 提交于 2019-12-06 19:21:25
问题 I have a multithreaded application, with an loop waiting for user input as main thread. On the correct input, it is supposed to stop the loop and wait for all other threads, to end propperly. For this purpose I created an std::list in which I put the std::future objects created for thread creation std::list<std::future<int>> threads; threads.emplace_front(std::async(std::launch::async, ...)); I was under the impression, that letting the list run out of scope, should block, until all threads

std::list containing std::function

南楼画角 提交于 2019-12-06 13:16:10
What I'm trying to achieve is std::list that contains std::functions . I'm trying to implement a callback system where functions can be added to the list and then the list can be looped through and each function called. What I have in class A is: std::list<std::function<void( bool )>> m_callbacks_forward; bool registerForward( std::function<void(bool)> f ) { m_callbacks_forward.push_back ( f ); return true; }; void GameControllerHub::invokeForward( bool state ) { for( std::list<std::function<void(bool)>>::iterator f = m_callbacks_forward.begin(); f != m_callbacks_forward.end(); ++f ){ f(); } }

C++ Linked list behavior

為{幸葍}努か 提交于 2019-12-05 10:32:18
I have some C code, where in there are two linked lists(say A and B) and A is inserted at a particular position into B and A still has elements. How do I simulate the same behavior effectively using the C++ STL? If I try splice, it makes the second one empty. Thanks, Gokul. You need to copy the elements. Consider something like this: std::copy(a.begin(), a.end(), std::inserter(b, b_iterator)); If you want the same nodes shared by two lists, this is simply not supported by std::list (STL containers always have exclusive ownership). You can avoid duplicating the elements by storing pointers in

std::list<std::future> destructor does not block

只愿长相守 提交于 2019-12-05 00:47:34
I have a multithreaded application, with an loop waiting for user input as main thread. On the correct input, it is supposed to stop the loop and wait for all other threads, to end propperly. For this purpose I created an std::list in which I put the std::future objects created for thread creation std::list<std::future<int>> threads; threads.emplace_front(std::async(std::launch::async, ...)); I was under the impression, that letting the list run out of scope, should block, until all threads return their main function, because the list s destructor will destrurct all std::future elements and

Does std::list::clear invalidate std::list::end iterator?

蹲街弑〆低调 提交于 2019-12-04 01:29:37
Check this code: #include "stdafx.h" #include <list> int _tmain(int argc, _TCHAR* argv[]) { std::list<int> mylist; mylist.push_back(1); std::list<int>::iterator i = mylist.end(); if( i == mylist.end() ) printf( "end is end\n" ); mylist.clear(); if( i == mylist.end() ) printf( "never get here because Microsoft seems to " "think the iterator is no longer safe.\n" ); return 0; } Now, according to cplusplus.com this shouldn't be a problem, and in release mode, I think this is fine and doesn't cause any issues really, but debugging becomes impossible as this just bails without letting me continue.

Destructor called on object when adding it to std::list

自古美人都是妖i 提交于 2019-12-03 12:44:39
问题 I have a Foo object, and a std::list holding instances of it. My problem is that when I add a new instance to the list, it first calls the ctor but then also the dtor. And then the dtor on another instance (according to the this pointer). A single instance is added to the list but since its dtor (along with its parents) is called, the object cant be used as expected. Heres some simplified code to illustrate the problem: #include <iostream> #include <list> class Foo { public: Foo() { int

Destructor called on object when adding it to std::list

落爺英雄遲暮 提交于 2019-12-03 02:59:30
I have a Foo object, and a std::list holding instances of it. My problem is that when I add a new instance to the list, it first calls the ctor but then also the dtor. And then the dtor on another instance (according to the this pointer). A single instance is added to the list but since its dtor (along with its parents) is called, the object cant be used as expected. Heres some simplified code to illustrate the problem: #include <iostream> #include <list> class Foo { public: Foo() { int breakpoint = 0; } ~Foo() { int breakpoint = 0; } }; int main() { std::list<Foo> li; li.push_back(Foo()); }

How to convert a sorted std::list of std::pair to a std::map

旧时模样 提交于 2019-12-01 01:54:49
问题 I have got a std::list< std::pair<std::string,double> > , which I know is sorted according to the std::string element . Since I would like to do a lot of std::find_if based on the std::string element, I believe a std::map<string,double,MyOwnBinaryPredicate> with lower_bound and upper_bound would be more adequate. The fact is that I want to insert elements in the std::map in an efficient way. So I want to use an additional iterator to make the insert faster. I believe the easiest way would be

What makes the gcc std::list sort implementation so fast?

南笙酒味 提交于 2019-11-30 12:42:00
I have a linked list implementation and I'm experimenting with both Mergesort and QuickSort algorithms. What I don't understand is why the sort operation in std::list is so fast. Looking at the std::list under linux and it appears to be linked list as well, not an array based list. The merge sort I tried almost identical to Dave Gamble's version here: Merge Sort a Linked List Also, I thought I'd try a simple quicksort based on this code: http://www.flipcode.com/archives/Quick_Sort_On_Linked_List.shtml Suprisingly, to sort 10 million random numbers using std::list and sort was around 10 times