Copy one string to the other in C++

Asked

Viewed 220 times

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.

  • What do you suggest? I understand very little, I’m still learning

  • I suggest learning in C++, do not do in C when you want to learn another language just because the compiler accepts.

1 answer

0

In c++ std::string already follows the lexicographic order. That is, just compare with the relational operators as <, >, etc. The only care you should take is that it differentiates between upper and lower case.

Already the function strcmp that you are using is to compare character arrays in C. There is no reason for you to use this function nor to call c_str().

Another point is the use of string vet[]. Here you are passing as an array of C objects std::string, which will actually degenerate to a pointer and therefore within the function particionaString you will not know the size of this array and need to pass this information (as you already do). Do not use pure C arrays. Use std::vector, std::array or another C++ library container so you avoid all this array complication by degenerating to pointer and C++ containers know their own size.

When exchanging values, you can also use the function std::swap, instead of manually creating the auxiliary variable and making the switch yourself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.