1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
////////////////////////////////////////////////////////////////
class phrase
{
private:
string texte;
public:
//--------------------------------------------------------------
//--------------------------------------------------------------
void displayphrase()
{
cout << texte;
}
//--------------------------------------------------------------
string getLast()
{ return texte; }
}; //end class phrase
////////////////////////////////////////////////////////////////
class ArrayInOb // L'erreur survient ici !!
{
private:
vector<phrase*> v;
int nElems;
public:
//--------------------------------------------------------------
ArrayInOb(int max) : nElems(0)
{
v.resize(max);
}
//--------------------------------------------------------------
void insert(string txt)
{
string texte;
v[nElems] = new phrase(texte);
nElems++;
}
//--------------------------------------------------------------
void display()
{
for(int j=0; j<nElems; j++)
v[j]->displayphrase();
}
//--------------------------------------------------------------
void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++)
{
phrase* temp = v[out];
in = out;
while( in>0 && v[in-1]->getLast() > temp->getLast() )
{
v[in] = v[in-1];
--in;
}
v[in] = temp;
} //end for
} //end insertionSort()
//--------------------------------------------------------------
}; //end class ArrayInOb
////////////////////////////////////////////////////////////////
int main()
{
int i,j;
int maxSize = 100;
ArrayInOb arr(maxSize);
//create array
arr.insert("John");
arr.insert("Smith");
arr.insert("Edward");
arr.insert("Yee");
cout << "Before sorting:" << endl;
arr.display();
arr.insertionSort();
cout << "After sorting:" << endl;
arr.display();
return 0;
} |