473,857 Members | 1,748 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 3707

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

Similar topics

37
7391
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
3870
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
5561
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
9801
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
2612
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
20925
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
2630
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
2796
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
6067
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
9923
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
9768
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10394
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...
0
9549
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7940
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
7104
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
5774
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5971
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3215
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.