stdlist

std::list containing std::function

对着背影说爱祢 提交于 2020-01-02 10:25:19
问题 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

Using delete on pointer to std::list?

你。 提交于 2019-12-24 19:28:01
问题 In my program I have a pointer to a std::list object, it is allocated like so. d_list_p = new std::list<some_type*>(); Then later in my program I delete it like so. d_list_p->clear(); delete d_list_p; For some reason I'm getting a Windows breakpoint triggered on the delete statement. If I break at the delete statement I see that the list exists and has a size of 0. Also, I never add an element to the list for the case that throws an error (I think). The code is being compiled with the MS VC++

How to achieve O(1) erasure from a std::list

喜你入骨 提交于 2019-12-23 09:15:44
问题 The question is what is the recommended way to use std::list to achieve O(1) erasure of list items? Usually, when I choose a doubly linked list, I want to be able to remove an element from a list in O(1) time, and then move it to a different list in O(1) time. If the element has its own prev and next pointers, there is no real trick to getting the job done. If the list is a doubly linked circular list, then removal doesn't necessarily require knowing the list that contains the item. As per

Find all matching elements in std::list

北慕城南 提交于 2019-12-23 07:50:34
问题 I was wondering if there's any built-in or well-established way (i.e. via lambda) to go through the elements of an std::list and find all the ones that match a given value? I know I can iterate through all of them, but I thought I'd ask if there's a way to get an iterator that iterates through just the elements that match a given criteria? My sample below only gives me the iterator to the first matching element. #include <list> #include <algorithm> #include <stdio.h> int main() { std::list

C++ Linked list behavior

江枫思渺然 提交于 2019-12-22 05:53:25
问题 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. 回答1: 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

C++ Return list inside a std::pair

China☆狼群 提交于 2019-12-13 09:09:59
问题 I have several elements held in a std::list . I have to output something like std::pair<int, std::list<std::string> > . For the int I just add the int variable in directly to the pair. Is there any way to print the list in one statement without having to use iterators? How do you do that inside a pair? 回答1: Is there any way to print the list in one statement without having to use interators. There sure is: If, while modifying the list, you maintain an array where you store pointers to each

std::list fixed size

那年仲夏 提交于 2019-12-12 14:25:24
问题 How can I create std::list with a fixed element count? 回答1: #include <list> // list with 5 elements, using default constructor const size_t fixedListSize(5); std::list<int> mylist(fixedListSize); If you want it to always have exactly 5 elements you'd have to wrap it in a facade class to prevent insertion and erasure. If that is indeed what you want, you'd be better off using a different container instead of list , since as noted in other responses you would be hiding the most advantageous

std::list implementation & pointer arithemetic.

纵饮孤独 提交于 2019-12-12 04:56:30
问题 As I understand it, std::vector allocates/de-allocates all the memory it requires each time it's elements grows or shrinks, therefore pointer arithmetic can be used to iterate the vector elements. std::list on the other hand uses a double linked list, with each element pointing to the next and previous element. Assuming(possibly wrongly) that std::list allocates it's memory dynamically, so memory is allocated, if and when required, incrementally. How is std::list still able to offer pointer

C++: can I reuse / move an std::list element from middle to end?

[亡魂溺海] 提交于 2019-12-11 16:33:36
问题 I'm optimising constant factors of my LRU-cache implementation, where I use std::unordered_map to store ::iterator s to std::list , which are guaranteed to remain valid even as nearby elements are added or removed. This results in O(n) runtime, so, I'm going after the constant factors. I understand that each iterator is basically a pointer to the structure that holds my stuff. Currently, to move a given element to the back of the linked list, I call l.erase(it) with the iterator, and then

Copying from map to a list of pointers

半世苍凉 提交于 2019-12-10 12:07:11
问题 I have this interesting assignment where I have a std::map of CTurist (previous class) and unsigned variable. Here's the code: class CTurist { protected: string tName; int age; public: CTurist() {}; CTurist(string name, int age2) { tName = name; age = age2; } bool operator<(const CTurist& e) const { return age < e.age; } friend ostream& operator<<(ostream& os, const CTurist&& e); friend ifstream& operator>>(ifstream& is, CTurist&& e); }; class CHotel:public CTurist { protected: string hName;