GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Dec-2008, 10:42
takachi takachi is offline
Awaiting Email Confirmation
 
Join Date: Dec 2007
Posts: 101
takachi is an unknown quantity at this point

Heaps using the vector class


hello.I've written a function which is supposed to get an index of a vector and checks for the value of that index in the vector if it is smaller than his children and if so than replaces between the two and calls the function again in recursion with the bigger son's index.the problem is that I get a segmentation fault when I try to activate the function.
here is the program:
CPP / C++ / C Code:
#include <iostream>
#include <vector>
using std::cout;
using std::endl;


class Heap {
		public:
		vector<int> HeapVec;

		void rebuildHeap(int index)
		{
			if (index*2<(int)HeapVec.size()) //if there are 2 sons
			{
				int maxSonIndex;
				if (HeapVec[2*index]>=HeapVec[2*index+1])//if the left son is bigger than the right one
					maxSonIndex=2*index;
				else
					maxSonIndex=2*index+1;
				if (HeapVec[maxSonIndex]>HeapVec[index]) //if the son is bigger than his father
				{	//replacing between son and father
					int temp=HeapVec[index];
					HeapVec[index]=HeapVec[maxSonIndex];
					HeapVec[maxSonIndex]=temp;
					rebuildHeap(maxSonIndex); //call again to function with the new son
				}
			}
			else if (index*2==HeapVec.size()) //if there is only one son
			{
				if (HeapVec[2*index]>HeapVec[index]) //if the son is bigger than his father
                		{       //replacing between son and father     
                        		int temp=HeapVec[index]; 
                        		HeapVec[index]=HeapVec[2*index];
                        		HeapVec[2*index]=temp;          
                        		rebuildHeap(2*index); //call again to function with the new son
       	         		}  
			}
		}

};
int main()
{
	int i;
	Heap h;
	h.HeapVec[0]=17;
	h.HeapVec[1]=11;
	h.HeapVec[2]=17;
	h.HeapVec[3]=10;
	h.HeapVec[4]=9;
	h.HeapVec[5]=12;
	h.HeapVec[6]=15;
	h.HeapVec[7]=6;
	h.HeapVec[8]=2;
	h.HeapVec[9]=8;
	h.HeapVec[10]=9;
	
	h.rebuildHeap(0);
	for(i=0;i<=10;i++)
		cout<<h.HeapVec[i]<<endl;



}

can you tell me where is the bugg or how to fix it because I really have no clue.
thank you in advance.
  #2  
Old 14-Dec-2008, 10:46
takachi takachi is offline
Awaiting Email Confirmation
 
Join Date: Dec 2007
Posts: 101
takachi is an unknown quantity at this point

Re: Heaps using the vector classs


I've found the error.but I have a qeustion:why can't I initialize a vector like an array? I've used push_back() instead but it pushes it to the top of the vector.
  #3  
Old 14-Dec-2008, 13:13
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Heaps using the vector classs


Quote:
Originally Posted by takachi
...why can't I initialize a vector like an array?...

The vector class has an "iterator consctuctor" that you can use to initialize a vector from an array:

CPP / C++ / C Code:
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int v_values[] = {11, 22, 33, 44, 55};
    int num_values = sizeof(v_values)/sizeof(v_values[0]);
    cout << "Number of elements in the v_values array = " << num_values << endl;

    //
    // The iterator constructor can convert array address values to interators
    //
    vector <int> ivector(v_values, v_values+num_values);
    cout << "ivector.size() = " << ivector.size() << endl;

    for (unsigned i = 0; i < ivector.size(); i++) {
        cout << "ivector[" << i << "] = " << ivector[i] << endl;
    }
    return 0;
}

Output
Code:
Number of elements in the v_values array = 5 ivector.size() = 5 ivector[0] = 11 ivector[1] = 22 ivector[2] = 33 ivector[3] = 44 ivector[4] = 55

Regards,

Dave
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Heaps and stacks!..what about pointers passed as arguments aijazbaig1 C Programming Language 4 04-Feb-2008 10:13
Non-local heaps and assertion errors Marius MS Visual C++ / MFC Forum 1 03-Sep-2004 06:45

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.