473,666 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to sort CArray<double, double> with quick sort

Hi,

I am using CArray and quick sort funciton to sort an array
of double type of data points. I found an article in MSDN

HOWTO: Quick Sorting Using MFC CArray-Derived Classes
ID: Q216858

The article works well with CStringArray. But when I wrote
the following code about double type. It does not work for
me, even though I use

class CSortableDouble Array : public
CArray<double,d ouble>

directly (without using CDoubleArray). The problem happens
when the second Add() is called:

tmp = 2.0;
MyArray.Add(tmp );

At this point, the program dies inside SetAtGrow() after

delete[] (BYTE*)m_pData;

I think this is the problem mentioned in Q216858. Could
anybody help?

Thank you very much.

fk

------------------- program -------------------------------

class CDoubleArray : public CArray<double,d ouble>
{
public:
CDoubleArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
~CDoubleArray()
{
}

CDoubleArray(co nst CDoubleArray& Src)
{
ASSERT_VALID(th is);
ASSERT(this != &Src);

SetSize(Src.m_n Size);

const double * pSrcValues = Src.GetData();
for (int j = 0; j <= Src.GetUpperBou nd();
j++)
{
m_pData[j] = pSrcValues[j];
}
}

const CDoubleArray& operator = (const
CDoubleArray& Src)
{
double * pValues = GetData();
for (int j = 0; j <= GetUpperBound() ; j++)
(*this).Add(pVa lues[j]);

return (*this);
}

protected:

};
typedef int (__cdecl *GENERICCOMPARE FN)(const void *
elem1, const void * elem2);
class CSortableDouble Array : public CDoubleArray
{
public:

void Sort(GENERICCOM PAREFN pfnCompare = Compare);
double GetMedian();

protected:
static int __cdecl Compare(const void * pValue1,
const void * pValue2);
};

int CSortableDouble Array::Compare( const void * pValue1,
const void * pValue2)
{
ASSERT(pValue1) ;
ASSERT(pValue2) ;
return ((*((double*)pV alue1)) > (*((double*)
pValue2)));
}

void CSortableDouble Array::Sort(GEN ERICCOMPAREFN
pfnCompare /*= CSortDoubleArra y::Compare */)
{
double * prgdbl = GetData();
qsort(prgdbl, GetSize(), sizeof(double),
(GENERICCOMPARE FN)pfnCompare);
}
nt main(int argc, char* argv[])
{

CSortableDouble Array MyArray;
double tmp = 1.0;
MyArray.Add(tmp );
tmp = 2.0;
MyArray.Add(tmp );
MyArray.Sort();
}
Nov 16 '05 #1
0 3687

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

Similar topics

37
7341
by: Zombie | last post by:
Hi, what is the correct way of converting contents of a <string> to lowercase? There are no methods of <string> class to do this so I fallback on strlwr(). But the c_str() method returns a const pointer which cannot be used with strlwr() as it does the conversion inplace. So, I use the following logic of copying the contents to a dynamically allocated char* array and then doing the conversion: -----------------------------
5
3860
by: Steve | last post by:
can someone tell me how qsort function in <stdlib.h> is used (qsort(..........))? the function has a buffer, two void * parameters and the a pointer to a compare function. Thanks.
9
5539
by: Eric Lindsay | last post by:
I can't figure how to best display little snippets of shell script using <pre>. I just got around to organising to bulk validate some of my web pages, and one of the problems occurs with Bash shell pieces like this: <pre><code> #!/bin/sh ftp -i -n ftp.server.com&lt; &lt;EOF user username password epsv4 cd /
2
9781
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator double() of class B is called for the member complex::operator*=(double) as expected, but not for operator*(complex, double). The effect is, that the template matching (or overload resolution)
4
2590
by: lutorm | last post by:
Hi all, I'm having a problem writing template functions that take vector<T>::iterator as arguments and I'm sure you guys can set me straight. Like this: #include<vector> using namespace std; template<typename T> void test2(typename std::vector<T>::iterator b)
20
20899
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement sorting for LinkedList<myself? Thanks in advance Martin
3
2618
by: Samuel R. Neff | last post by:
You can you program against generic interfaces generically? For example, how can I make the following code which works for the non-generic interface also work for the generic counterparts? public bool Equivalent(ArrayList x, ArrayList y) { if (x is IComparable) { x.Sort(); x.Sort(); }
2
2781
by: jabbah | last post by:
I have some data in a map and I want to sort it. Currently I have implemented it like this: #include <iostream> #include <map> #include <string> using namespace std; int main(){
5
6046
by: jeremit0 | last post by:
I'm trying to sort a vector<complex<double and can't figure it out. I recognize the problem is that there isn't a default operator< for complex data types. I have written my own operator and can use it, but std::sort doesn't seem to find it. I have copied a very simple example below. Everything compiles just fine when the line with std::sort function is commented out, but with that line included a whole slew of errors are given like: ...
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.