The thing is, I don't know what's the "Iterator" doing exactly and what's begin/end, how can I use arrays with the former piece of code?

Any help -in c++- will be appreciated :D

June 3rd, 2012, 10:58 AM

laserlight

Re: Array to Heap Tree

You would only need to use make_heap, methinks. The begin/end iterators of an array would be a pointer to the first element of the array and a pointer to one past the last element of the array, respectively.

June 3rd, 2012, 11:21 AM

ahoodin

Re: Array to Heap Tree

An Iterator is like a pointer to an array element. To iterate is to step through to the next element in the array. MyIterator++ would iterate to the next element. A heap is a binary tree that is in order from highest priority (or value) to least. The priority Queue is implemented as a heap, where you pop the highest priority or value item off the top of a heap, and then reorder the remaining elements into another heap. Note there are a few more rules to priority queue than that, but hope it gives you some more hints about your heap.

Quote:

The thing is, I don't know what's the "Iterator" doing exactly and what's begin/end, how can I use arrays with the former piece of code?

how can I use pop_heap and push_heap in the same manner with the same array without using template stuff.

June 3rd, 2012, 11:54 AM

laserlight

Re: Array to Heap Tree

Quote:

Originally Posted by R1111

how can I use pop_heap and push_heap in the same manner with the same array without using template stuff.

What is the bigger picture of what you are trying to do?

Do you understand what pop_heap and push_heap do? If not, read stuff like cppreference.com's entries on pop_heap and push_heap.

June 3rd, 2012, 12:25 PM

R1111

Re: Array to Heap Tree

I want to add values to my array, push_heap should do it, I don't know how to use it. The example on the reference page uses vectors and "auto." Do you know what to do/how to use push_heap in a simple clean code like the above?

thanks

June 3rd, 2012, 12:34 PM

laserlight

Re: Array to Heap Tree

Quote:

Originally Posted by R1111

I want to add values to my array, push_heap should do it, I don't know how to use it.

You cannot add values to your array because your array's size is fixed at compile time. What you can do is create an array as large as you will ever need and then just use part of it incrementally, but...

Quote:

Originally Posted by R1111

The example on the reference page uses vectors and "auto." Do you know what to do/how to use push_heap in a simple clean code like the above?

Using a vector would be appropriate. You don't need to use auto here (it was merely used as a convenience, as intended, in printing the vector's contents).

Besides, do you understand what the example does? If you understand, you can apply it to an array. If you don't understand... what exactly don't you understand? (That is, I can spoonfeed you code, but if you don't understand it, then it is a pointless exercise.)

June 3rd, 2012, 12:37 PM

R1111

Re: Array to Heap Tree

well, it's confusing, there's this "v.push_back(value)" with vectors, but what's the equivalent if I'm using an array instead of a vector?

June 3rd, 2012, 12:39 PM

laserlight

Re: Array to Heap Tree

Quote:

Originally Posted by R1111

well, it's confusing, there's this "v.push_back(value)" with vectors, but what's the equivalent if I'm using an array instead of a vector? .

There is no equivalent. You cannot increase the size of an array. As I mentioned, you could create an array as big as you will ever need then incrementally use it, but unless you have special and convincing reasons to avoid std::vector, it is simpler to just use std::vector.

June 3rd, 2012, 12:43 PM

R1111

Re: Array to Heap Tree

Assuming that I defined a bigger array, and only a few values are associated with the first few slots. How can I add values using push_heap, if there's no equivalent to v.push_back(value) ?

June 3rd, 2012, 12:48 PM

laserlight

Re: Array to Heap Tree

Quote:

Originally Posted by R1111

Assuming that I defined a bigger array, and only a few values are associated with the first few slots. How can I add values using push_heap, if there's no equivalent to v.push_back(value) ?

Simple: you pass a pointer to one past the newest array element in use as the second argument to push_heap.

Or, let me ask you: what do you understand by the statement that push_heap "inserts the element at the position last-1 into the heap defined by the range [first, last-1)"?

June 3rd, 2012, 01:01 PM

R1111

Re: Array to Heap Tree

Quote:

Originally Posted by laserlight

Or, let me ask you: what do you understand by the statement that push_heap "inserts the element at the position last-1 into the heap defined by the range [first, last-1)"?

It inserts the value to the left of the last level leaf?

Quote:

Simple: you pass a pointer to one past the newest array element in use as the second argument to push_heap.

but where do I put the value that I want to add? let's say I want to add number 50?
:confused:

June 3rd, 2012, 01:06 PM

laserlight

Re: Array to Heap Tree

Quote:

Originally Posted by R1111

It inserts the value to the left of the last level leaf?

No, that is not what it means. Rather, it states that the element at that position in the range (i.e., the range of the array, vector, etc) is inserted into the heap that consists of the elements of the range.

Quote:

Originally Posted by R1111

so would it be something like this?

No, because your array has a size of 7, and all 7 elements of the array are in use. Furthermore, push_heap only works if the rest of the range is a heap. For this scheme to work, you need to keep track of the number of elements in use. What's stopping you from using a std::vector?

June 3rd, 2012, 01:19 PM

R1111

Re: Array to Heap Tree

I don't know how to deal with vectors, first encounter! I'm trying to use what I'm somewhat familiar with. I'll try to learn vectors soon.