473,395 Members | 1,905 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,395 software developers and data experts.

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 CSortableDoubleArray : public
CArray<double,double>

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,double>
{
public:
CDoubleArray()
{
m_pData = NULL;
m_nSize = m_nMaxSize = m_nGrowBy = 0;
}
~CDoubleArray()
{
}

CDoubleArray(const CDoubleArray& Src)
{
ASSERT_VALID(this);
ASSERT(this != &Src);

SetSize(Src.m_nSize);

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

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

return (*this);
}

protected:

};
typedef int (__cdecl *GENERICCOMPAREFN)(const void *
elem1, const void * elem2);
class CSortableDoubleArray : public CDoubleArray
{
public:

void Sort(GENERICCOMPAREFN pfnCompare = Compare);
double GetMedian();

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

int CSortableDoubleArray::Compare(const void * pValue1,
const void * pValue2)
{
ASSERT(pValue1);
ASSERT(pValue2);
return ((*((double*)pValue1)) > (*((double*)
pValue2)));
}

void CSortableDoubleArray::Sort(GENERICCOMPAREFN
pfnCompare /*= CSortDoubleArray::Compare */)
{
double * prgdbl = GetData();
qsort(prgdbl, GetSize(), sizeof(double),
(GENERICCOMPAREFN)pfnCompare);
}
nt main(int argc, char* argv[])
{

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

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

Similar topics

37
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...
5
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
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...
2
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...
4
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; ...
20
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...
3
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? ...
2
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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...

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.