0
I am doing a school activity where I have a list of book titles. Each title stored in a string field of a structure. I need to sort this list alphabetically through the quicksort where I should also implement the algorithm. My problem is exactly the exchange of values. I use the strcmp function to compare the strings. I am doing so but not this one:
int Ordenacao::particionaString (string vet[], int ini, int fim)
{
int esquerda = ini+1;
int direita = fim;
string pivo = vet[ini];
string aux;
int n = 0;
while(esquerda < direita){
n = strcmp(vet[esquerda].c_str(), pivo.c_str()); // < 0 a primeira e menor
while(n < 0){
esquerda++;
n = strcmp(vet[esquerda].c_str(), pivo.c_str());
}
n = strcmp(vet[direita].c_str(), pivo.c_str());
while(n >0){
direita--;
n = strcmp(vet[direita].c_str(), pivo.c_str());
}
if(esquerda < direita){
cout << "Trocando " << vet[esquerda] << " com " << vet[direita] << endl;
aux = vet[esquerda];
vet[esquerda] = vet[direita];
vet[direita] = aux;
}
}
vet[ini] = vet[direita];
vet[direita] = pivo;
return direita;
}
I cannot use the strcpy function of the string library because it asks for a char* on the destination and I am working with string. Assignments with = are giving error.
How can I solve this problem?
And why make all this confusion? If you are programming in C++. make a code C++, do not use C, After all is inefficient.
– Maniero
What do you suggest? I understand very little, I’m still learning
– Felipe Augusto
I suggest learning in C++, do not do in C when you want to learn another language just because the compiler accepts.
– Maniero