Est-ce que ous connaissez une class C++ qui permet de gérer une liste d'éléments comme le fait QPtrList mais sans Qt?
------------------------------------
=> Les sources de ce que j'ai fait + les corrections données dans les posts #endif
CList.h : // --------------------------------------------------------------------------------------- //
// C++ Class //
// Copyright (c) 2006 Spomky.Dev //
// <http://www.spomky.com/> //
// ---------------------------------------------------------------------------------------- //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// You may not change or alter any portion of this comment or credits //
// of supporting developers from this source code or any supporting //
// source code which is considered copyrighted (c) material of the //
// original comment or credit authors. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
// ---------------------------------------------------------------------------------------- //
// Author: Webmaster/Spomky.Dev (webmaster@spomky.com)
// URL: http://www.spomky.com
// Project:
//HEADER
#ifndef CLIST_H
#define CLIST_H
#include <cstdlib>
template <class Type>
class CList
{
public:
CList( void );
~CList( void );
bool addElement( void );
bool addElement( Type& );
bool clearList( void );
bool removeElement( size_t );
bool changeElement( size_t , Type& );
bool swapElement( size_t , size_t );
Type getElement( size_t );
size_t count( void ) const { return this->number; };
bool is_empty( void ) const { return this->count() == 0; };
protected:
private:
Type **list;
size_t number;
};
#include "CList.cpp"
}
#endif
CList.cpp : // --------------------------------------------------------------------------------------- //
// C++ Class //
// Copyright (c) 2006 Spomky.Dev //
// <http://www.spomky.com/> //
// ---------------------------------------------------------------------------------------- //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// You may not change or alter any portion of this comment or credits //
// of supporting developers from this source code or any supporting //
// source code which is considered copyrighted (c) material of the //
// original comment or credit authors. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
// ---------------------------------------------------------------------------------------- //
// Author: Webmaster/Spomky.Dev (webmaster@spomky.com)
// URL: http://www.spomky.com
// Project:
//C++ CODE SOURCE
#include "CList.h"
#ifndef CLIST_CPP
#define CLIST_CPP
template <class Type>
CList<Type>::CList( void )
{
this->number = 0;
this->list = NULL;
}
template <class Type>
CList<Type>::~CList( void )
{
this->clearList();
}
template <class Type>
bool CList<Type>::addElement( void )
{
if ( this->number == 0 )
this->list = (Type**)malloc(sizeof(Type*));
else
this->list = (Type**)realloc( this->list , sizeof(Type*) * (this->number+1) );
if ( this->list == NULL )
return false;
this->list[this->number] = new Type;
this->number++;
return true;
}
template <class Type>
bool CList<Type>::addElement( Type &newElement )
{
if ( this->addElement() )
*this->list[this->number-1] = newElement;
else
return false;
return true;
}
template <class Type>
bool CList<Type>::clearList( void )
{
if ( this->is_empty() )
return true;
while ( !this->is_empty() )
removeElement(0);
return true;
}
template <class Type>
bool CList<Type>::removeElement( size_t element )
{
if ( this->count() == 0)
return true;
if ( this->number == 1 )
{
free(this->list[0]);
free ( this->list );
this->list = NULL;
}
else
{
free(this->list[element]);
this->list[element] = NULL;
for (size_t cptr=element ; cptr < this->number-1 ; cptr++)
this->list[cptr] = this->list[cptr+1];
this->list = (Type**)realloc( this->list , (this->number-1) * sizeof(Type*) );
}
this->number--;
return true;
}
template <class Type>
bool CList<Type>::changeElement( size_t element , Type &source )
{
if ( element >= this->count() )
return false;
*this->list[element] = source;
return true;
}
template <class Type>
bool CList<Type>::swapElement( size_t element1 , size_t element2 )
{
if ( element1 >= this->count() || element1 >= this->count() )
return false;
Type temp;
temp = *this->list[element1];
*this->list[element1] = *this->list[element2];
*this->list[element2] = temp;
return true;
}
template <class Type>
Type CList<Type>::getElement( size_t element )
{
Type tempElement;
if ( element >= this->count() )
return tempElement;
return *this->list[element];