delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/13/10:54:56

From: authentic AT tip DOT nl (Rick)
Newsgroups: comp.lang.c++,comp.os.msdos.djgpp
Subject: list.sort(const void*(*fnc)()) again;=)
Date: Sun, 12 Apr 1998 12:04:31 GMT
Organization: NL-NIC
Lines: 156
Message-ID: <6gsv3d$dli$1@cadmium.aware.nl>
Reply-To: authentic AT tip DOT nl
NNTP-Posting-Host: nijmegen-110.trb.pop.tip.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Dear c++ programmers,

Code snippet below is a simplified sample of my
questions:
1. Am I right that when I have instantiated
   more then one objects of myclass that only
   one function get_myname and one function
   get_myid is created(it should be...).
2.. How will I arrive at the pointer to that function ?
3. How do I reference that pointer inside my template
    list ?(the example below)

I have a mistake in either 2 or 3 I think..If someone can
give me a hand, I would appreciate...

4. I think I need this kind of thing when I want to 
    cut down on the sort/compare functions. Do
    you know or see another solution ?

Thanks,

Rick


#include <iostream.h>
#include <stl.h>


class myclass
{

        public:

        myclass(char* _myname=0,
                     int _myid=0):myname(_myname),myid(_myid){};
        friend ostream& operator<<(ostream& os,myclass _myclass);
        friend int operator<(myclass el2,myclass el1);

        int operator>(myclass el2)
           {
            return (myid > el2.myid)>0 ? 1:0;
           }

        const char* get_myname()const{return myname;}
        const int   get_myid()const {return myid;}

        private:
        char* myname;
        int   myid;

};

ostream& operator<<(ostream& os, myclass _myclass)
{
         os<<_myclass.myname<<'\t'<<_myclass.myid<<endl;
}

int operator<(myclass el1, myclass el2)
{
        return (el1.get_myid() < el2.get_myid())<0 ? 0:1;
}

template <class T>
class mylist:public list<T>
{
        public:
        //overladen van ingebouwde sort: dat is alles
        void sort2(const void* (T::*get_function)()const);
        //Om dat te doen moet je dus eigenlijk de call
        //de merge overladen(die gebruikt operator < en !=)
        void merge(list<T>&,const void* (T::*get_function)()const);

};

//below is a simple adjustment from the wonderful
//stl codes in gcc 

template <class T>
void sort2(const void* (T::*get_function)()const)
{

    if (size() < 2) return;
    list<T> carry;
    list<T> counter[64];
    int fill = 0;
    while (!empty()) {
	carry.splice(carry.begin(), *this, begin());
	int i = 0;
	while(i < fill && !counter[i].empty()) {
	    counter[i].merge(carry,get_function);
	    carry.swap(counter[i++]);
	}
	carry.swap(counter[i]);         
	if (i == fill) ++fill;
    } 

    for (int i = 1; i < fill; ++i)
counter[i].merge(counter[i-1],get_function);
    swap(counter[fill-1]);
}

template <class T>
void const merge(list<T>& sourcelist,const void*
(T::*get_function)()const)
{

    iterator first1 = begin();
    iterator last1 = end();
    iterator first2 = x.begin();
    iterator last2 = x.end();
    while (first1 != last1 && first2 != last2)

       if (first2->get_function < first1->get_function) {
	    iterator next = first2;
	    transfer(first1, first2, ++next);
	    first2 = next;
	} else
	    ++first1;
    if (first2 != last2)
       transfer(last1, first2, last2);
    length += x.length;
    x.length= 0;
}


int main()
{
        mylist<myclass> lijst1;
        ostream_iterator<myclass>stdout(cout);
        mylist<myclass>::iterator i;

        myclass temp("Annet",1);
        lijst1.push_front(temp);
        lijst1.push_front(myclass("Patty",2));
        lijst1.push_front(myclass("Han",3));
        lijst1.push_front(myclass("Harry",4));

	//BELOW is the problem: I can't simply get 
	//to the pointer to pass(changing the void* return
             //to char* return is only changing errormessages..

        //Hier is 'tie nie oke
        //const void* (myclass::*get_fnc)()const=myclass::get_myname;
        //lijst1.sort2(get_fnc);

        lijst1.sort();
        copy(lijst1.begin(),lijst1.end(),stdout);



        return 0;

}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019