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 04-Oct-2008, 23:09
styxx22 styxx22 is offline
New Member
 
Join Date: Oct 2008
Posts: 2
styxx22 is on a distinguished road

Operator Overloading & Class implementation Question


Hello GID community... I am in need of some implementation help. As well with some operator overloading help.

I have many many sorting algorithms that I have created over the last week, and now came to a point I want to count for each one how many comparisons within the code. Easiest way to do it looks like operator overloading.

I have searched up and down to find this out. I have used BOOST libraries to extensively test the sorts I have created and now I am onto this.

CPP / C++ / C Code:
class CAnalyzeSorts
{
private:
unsigned m_CountComparisons;
public:

unsigned getCountComparisons();

// How would I implement this to "count" the comparison
// I have a ton of sorts to count - so counting each one is key
const T operator == ( T const& rh) {
}

//Other comparison operators....


/** @fn void bubbleSort(std::vector<int> &vecToSort)
@brief Sort using bubble algorithm
@param vector of elements to sort 
@return void **/
template <typename T>
	void selectionSort(std::vector<T> &vecToSort)
	{
		std::vector<T>::iterator i, j;
		
		// For each number starting at the beginning of the vecToSort [ START ... MAX ]
		for ( i = vecToSort.begin(); i != vecToSort.end(); ++i )
			// ... Look at the proceeding number until it is larger than the one we are currently looking at
			for( j = i; j != vecToSort.end(); ++j )
				// ... If the current number we were looking at is smaller than the proceeding number so swap the pair
				if (*j < *i)
					std::iter_swap(i,j);
	}
//Other sorts will be implemented...
};

The question I have is, how do I count the comparisons using overloading in a class like that? Am I thinking correctly or would there be a better way to do this? Is there a way to overload the comparison operators for a templated type?
  #2  
Old 05-Oct-2008, 00:08
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: Operator Overloading & Class implementation Question


Quote:
Originally Posted by styxx22
The question I have is, how do I count the comparisons using overloading in a class like that?
Assuming that where you are wanting to count is at the following line:
Quote:
CPP / C++ / C Code:
if (*j < *i)
Recognize that you will only be able to overload operator<() for user-defined classes, not fundamental types such as float, char, or int.
Quote:
Am I thinking correctly or would there be a better way to do this?
I suspect you should wrestle more with the code in order to more clearly define the question to yourself.
Quote:
Is there a way to overload the comparison operators for a templated type?
Recognize that it is the instantiation type of the template class which will need to overload operator<().
  #3  
Old 05-Oct-2008, 00:33
styxx22 styxx22 is offline
New Member
 
Join Date: Oct 2008
Posts: 2
styxx22 is on a distinguished road

Re: Operator Overloading & Class implementation Question


"Recognize that you will only be able to overload operator<() for user-defined classes, not fundamental types such as float, char, or int."

That helped me thank you.

I now have created a new type with it's own overloaded operators. I will now sort using the new user defined type.

Best Regards.
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

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
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 15:11
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
Opinion on my code and a c++ class question FlipNode C++ Forum 7 07-Feb-2006 08:15

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

All times are GMT -6. The time now is 16:02.


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