473,396 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Operator <

I have this code that I am using to create a heap
template <class t>
void BinaryHeap<t>::insert( const T &x)
{
int hole = ++ currentSize;
for ( ; hole > 1 && x < array[hole/2];hole /= 2)
arrsy[hole] = arrsy[hole/2];

array[hole] =x;

}

I am not allowed to change this code. I want to use the binary heap as
a queue to store Huffman nodes(trees).
How would I do it so that the comparison in the for loop is correct?

Nov 26 '05 #1
3 1271
peak wrote:
I have this code that I am using to create a heap
template <class t>
void BinaryHeap<t>::insert( const T &x)
{
int hole = ++ currentSize;
for ( ; hole > 1 && x < array[hole/2];hole /= 2)
arrsy[hole] = arrsy[hole/2];

array[hole] =x;

}

I am not allowed to change this code. I want to use the binary heap as
a queue to store Huffman nodes(trees).
How would I do it so that the comparison in the for loop is correct?


You're not allowed to change the code, but you want to correct the for
loop. Huh?

Are you saying that you need to write an operator< ? I'm confused,
please clarify.

Also you have some spelling mistakes (arrsy instead of array) so you are
gonig to have to make some changes.

john
Nov 26 '05 #2
peak wrote:
I have this code that I am using to create a heap
template <class t>
void BinaryHeap<t>::insert( const T &x)
{
int hole = ++ currentSize;
for ( ; hole > 1 && x < array[hole/2];hole /= 2)
arrsy[hole] = arrsy[hole/2];

array[hole] =x;

}

I am not allowed to change this code. I want to use the binary heap as
a queue to store Huffman nodes(trees).
How would I do it so that the comparison in the for loop is correct?


Aside from the spelling errors that John pointed out it's not clear to
me what your problem is. Your code appears to represent a binary heap
as a tree in array form and appears to do the right things required when
inserting an object into the heap.

Of course it's required that operator< be defined for items of type T.
You can define these yourself as needed in one of two ways:

// non-member function
bool operator< (const T& lhs, const T& rhs) { ... }

// member function
class T
{
...
bool operator< (const T& rhs) const { ... }
...
};
Nov 26 '05 #3
>
Of course it's required that operator< be defined for items of type T.
You can define these yourself as needed in one of two ways:

// non-member function
bool operator< (const T& lhs, const T& rhs) { ... }

// member function
class T
{
...
bool operator< (const T& rhs) const { ... }
...
};


The non-member function is to be preferred for reasons that are hard to
explain to a newbie. So trust me write a non-member function, but see
the FAQ if interested.

http://www.parashift.com/c++-faq-lit....html#faq-13.9

point 7 is applicable here.

john

Nov 26 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
3
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
4
by: bluekite2000 | last post by:
Here A is an instantiation of class Matrix. This means whenever user writes Matrix<float> A=rand<float>(3,2);//create a float matrix of size 3x2 //and fills it up w/ random value cout<<A; the...
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
6
by: johnmmcparland | last post by:
Hi all, when I write a subclass, can I inherit its superclass' << operator? As an example say we have two classes, Person and Employee. class Person has the << operator; /**
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.