heap

Priority queue with dynamic item priorities

馋奶兔 提交于 2019-12-17 19:33:46
问题 I need to implement a priority queue where the priority of an item in the queue can change and the queue adjusts itself so that items are always removed in the correct order. I have some ideas of how I could implement this but I'm sure this is quite a common data structure so I'm hoping I can use an implementation by someone smarter than me as a base. Can anyone tell me the name of this type of priority queue so I know what to search for or, even better, point me to an implementation? 回答1: I

What is the difference between the heap and the free store? [duplicate]

独自空忆成欢 提交于 2019-12-17 17:53:11
问题 This question already has answers here : C++, Free-Store vs Heap (7 answers) Closed last year . Originally a pedantics war on @Als answer here, it also sparked a discussion in the C++ chatroom. This article by Herb Sutter distinguishes between the two, but is also over a decade old, as it clearly was written before 2000 and also talks about the standard draft , which can only mean C++98 draft. Though, I still expect Herb, as part of the committee, to be knowledgeable about this stuff. I know

What are the benefits of a binary heap with root at arr[0]

青春壹個敷衍的年華 提交于 2019-12-17 17:23:20
问题 I am writing a binary heap over an array arr . Every node except the leaf nodes have two children. The root can be at arr[0] or arr[1] . The accepted answer at Why in a heap implemented by array the index 0 is left unused? says arr[1] is faster. But one comment below that answer says most implementation put root at arr[0] . What are the benefits of putting the root at arr[0] ? 回答1: I am the person who answered the question you linked. Creating a binary heap that has the root at arr[1] in a

What's the advantage of using std::allocator instead of new in C++?

▼魔方 西西 提交于 2019-12-17 15:35:42
问题 I've just read about std::allocator . In my opinion, it is more complicated to use it instead of using new and delete . With allocator we must explicitly allocate heap memory, construct it, destroy it, and then finally deallocate the memory. So why was it created? In which cases can it be used and when should it be used instead of new and delete? 回答1: std::allocator is the default memory allocator for the standard library containers, and you can substitute your own allocators. This allows you

Deleting a heap then dereferencing a pointer to that memory

我的梦境 提交于 2019-12-17 14:23:08
问题 This is code from an exercise: #include <iostream> using namespace std; int main() { int n = 13; int* ip = new int(n + 3); int* ip2 = ip; cout << *ip << endl; delete ip; cout << *ip2 << endl; cout << ip << tab << ip2 << endl; } When the space allocated to the int on the heap is deleted, I thought that dereferencing the pointer would give some sort of memory error. Instead, it returns 0. Why is this? 回答1: Dereferencing an invalid pointer leads to undefined results per spec. It's not guaranteed

Deleting a heap then dereferencing a pointer to that memory

耗尽温柔 提交于 2019-12-17 14:22:09
问题 This is code from an exercise: #include <iostream> using namespace std; int main() { int n = 13; int* ip = new int(n + 3); int* ip2 = ip; cout << *ip << endl; delete ip; cout << *ip2 << endl; cout << ip << tab << ip2 << endl; } When the space allocated to the int on the heap is deleted, I thought that dereferencing the pointer would give some sort of memory error. Instead, it returns 0. Why is this? 回答1: Dereferencing an invalid pointer leads to undefined results per spec. It's not guaranteed

Encourage the JVM to GC rather than grow the heap?

妖精的绣舞 提交于 2019-12-17 10:46:19
问题 (Note that when I say "JVM", I really mean "Hotspot", and I'm running the latest Java 1.6 update.) Example situation: My JVM is running with -Xmx set to 1gb. Currently, the heap has 500mb allocated, of which 450mb is used. The program needs to load another 200 mb on the heap. Currently, there is 300mb worth of "collectable" garbage in the heap (we'll assume it's all in the oldest generation.) Under normal operation, the JVM will grow the heap to 700 mb or so, and garbage collect when it gets

how to increase java heap memory permanently?

喜欢而已 提交于 2019-12-17 10:44:58
问题 I have one problem with java heap memory. I developed one client server application in java which is run as a windows service it requires more than 512MB of memory. I have 2GB of RAM but when I run my application it throws an exception Out of memory error:java heap space but I have already set heap size (maximum 512MB) in the java control panel and I still get the same error. I can't set heap size through the command line because my application runs as a windows service so how can I increase

The reason of using `std::greater` for creating min heap via `priority_queue`

不羁的心 提交于 2019-12-17 09:41:11
问题 I am wondering why for creating a min heap using the priority_queue , the std::greater should be used? std::priority_queue<T, std::vector<T>, std::greater<T> > min_heap; To me, since the smallest value is always located at the top of the heap, the employed class should be std::less Update: On the other hand, since the default behavior of priority_queue (max heap) is to hold the greatest value at the top, it looks to me that the std::greater should be used for the max heap creation and not for

What is the origin of the term “heap” for the free store?

心已入冬 提交于 2019-12-17 09:33:29
问题 I am trying to find the official (or a good enough) reason that the free store is commonly referred to as the heap. Except for the fact that it grows from the end of the data segment, I can't really think of a good reason, especially since it has very little to do with the heap data structure. Note: Quite a few people mentioned that it's just a whole bunch of things that are kind of unorganized. But to me the term heap physically means a bunch of things that are physically dependent on one