Cpp

Destructeurs virtuels

Attention à bien mettre le destructeur d'une classe en virtual

Recopier une map

ex: recopier map_src dans map_dest

copy(map_src.begin(), map_src.end(),  std::inserter(map_dest, map_dest.begin()));

Améliorer le template bind2nd

J'ai l'erreur

error C2529: '_Right' : reference to reference is illegal      
C:\Program Files\Microsoft Visual Studio 8\VC\ce\include\functional


static_cast dynamic_cast

Voir Postfix expressions

Faire un split de chaine

Comment faire un tri d'un tableau de pointeur d'objets

J'ai trouvé la solution à partir de An array can use stl functions C++ en utilisant les fonctions standards stl.

  • exemple


#include <algorithm>
use namespace std;

class Leaf
{
public:
        Leaf(int tail) { taille = tail;};
        int taille;
};

bool mysort (Leaf *pL1, Leaf *pL2)
{
        return (pL1->taille < pL2->taille);
}

void testsort()
{
        Leaf a(4), b(1), c(3),d(2);
        int n;
        Leaf **pLeaf = new Leaf*[4];
        Leaf **pBegin;
        Leaf **pEnd;
        pLeaf[0] = &a;
        pLeaf[1] = &b;
        pLeaf[2] = &c;
        pLeaf[3] = &d;
        pBegin = &pLeaf[0];
        pEnd = &pLeaf[4];
        sort(pBegin, pEnd, mysort);
        for (int i = 0; i < 4; i++)
                std::cout << "i:" << i << ":" << pLeaf[i]->taille << "\n";
        std::cin >> n;

        //desallocation
        for (int i = 0; i < 4; i++)
                delete [] pLeaf[i];

}




vector<Leaf*> pLeaf;
pLeaf->resize(4);
pLeaf[0] = &a;
pLeaf[1] = &b;
pLeaf[2] = &c;
pLeaf[3] = &d;
sort(pLeaf->begin(), pLeaf->end(), mysort);




class Leaf
{
public:
        Leaf(int tail) { taille = tail;};
        int taille;
        struct compare : public binary_function<Leaf&, Leaf&, bool>
        {
                bool operator()( Leaf* lhs, Leaf* rhs)
                {
                        return (lhs->taille < rhs->taille);
                }
        };
}

...
sort(pLeaf, pLeaf+4, Leaf::compare());
...




class Leaf
{
...
static int mysort (const void *pL1, const void *pL2)   
        {
                return (((Leaf*)pL1)->taille < ((Leaf*)pL2)->taille);
        }
}
et
...
sort(pLeaf, pLeaf+4, &Leaf::mysort);




int mysort (const void *pL1, const void *pL2)
{
        return ((*(Leaf**)pL1)->taille > (*(Leaf**)pL2)->taille);
}

...
qsort(pLeaf,4,sizeof(pLeaf[0]), mysort);



Comment faire une allocation d'un tableau dynamique à n dimensions

  • Exemple avec un tableau 2 * 3 * nord+1 * nord+1
typedef double arr[2][3];
arr init = {1,2,3,4,5,6};
 
testdynamic()
{
	arr **dynamicArray;
	int nord = 1;
	dynamicArray = new arr*[nord+1];
	for (int i = 0; i < nord+1; i++)
		dynamicArray[i] = new arr[nord+1];
	for (int i = 0; i < nord+1; i++)
		for (int j = 0; j < nord+1; j++)
			memcpy(dynamicArray[i][j],init, sizeof(init));
 
	for (int i = 0; i < nord+1; i++)
		for (int j = 0; j < nord+1; j++)
			for (int k = 0; k < 2; k++)
				for (int l = 0 ; l < 3; l++)
					std::cout << "arr[" << i << ":" << j << ":" <<k << ":"
                                                     << l << "]" << dynamicArray[i][j][k][l] << "\n";
	std::cin >> nord;
	//desallocation
	for (int i = 0; i < nord+1; i++)
		delete [] dynamicArray[i];
	delete[] dynamicArray;
	return 0;
}

Liens Externes