I haven’t written in c ++ for a long time, this problem arose

There is an array of pointers to class objects, say Class1 .

It is necessary to sort the array in ascending order; when sorting, it is natural to have to rearrange elements in places, i.e. one pointer to assign the value of another, please tell me how to do it. I do about the following:

 Class1* Array = new Class1[n]; Class1* temp = &Array[j+1]; // здесь все хорошо Array[j+1] = &Array[j]; // а как быть тут? Array[j] = temp; // и тут? 

In the 2nd and 3rd line of the code, it turns out that the left-handed pointer, i.e. the object itself and the link to the right

How do you yourself refer to the left side as a pointer?

    4 answers 4

    • You have brought not an array of pointers, but an array of objects, hence the difficulty. In general, your problem can be solved in two ways.

    • The first is that you begin to honestly store arrays of pointers to objects:

       Class1** array = new Class1*[n]; // Лучше в таких случаях пользоваться 'std::vector<Class1*>', // а еще лучше - 'std::vector<std::shared_ptr<Class1>>'. // ... std::swap(array[0], array[1]); 
    • The second is that you give your objects the semantics of CopyConstructible and then the exchange takes place by implicitly copying:

       CopyConstructibleClass1* array = new CopyConstructibleClass1[n]; // ... std::swap(array[0], array[1]); 

      @ Sergey041691 , judging by the comments, you are simply being led away from the simplest solution. Actually, @ Kotik wrote to you immediately: "you start to honestly store arrays of pointers to objects".

      To make it clearer, look at pure C (g ++ also interprets this code as C ++)

       char *array_of_Cstrings[] = {"str1", "str2", "str3"}; // массив из 3-х указателей char *t; // указатель // обменяем первый и последний элементы массива int last = sizeof(array_of_Cstrings)/sizeof(array_of_Cstrings[0]) - 1; t = array_of_Cstrings[0]; array_of_Cstrings[0] = array_of_Cstrings[last]; array_of_Cstrings[last] = t; 

      That's all, as you can see is very simple.

      The rest is invented with 3 goals

      1. allow relatively easy to create problem-oriented dialects of the language
      2. help the compiler find more possible errors in them
      3. raise the wages of connoisseurs of the crosses (there is such a bad joke)

        Since the type "Class1" is the type you created, you could wrap it in some container (or even container adapter) or STL (well, it's C ++). Personally, I advise you to use the container priority_queue<Class T> adapter and reload several operators for the class, namely: <,>, ==. In this case, you won't even have to sort anything - the container will do it for you.


        If, for political reasons, the adapters do not like, then you can use, for example, the container List<Class T> . Overloading, again, with some logical operators. Then sort the container either using алгоритма sort() , or using the same sort() , but at the level of the List container itself, if it is selected.

        • @Asen, and how is it >> Reload several operators? Can "overload"? :) - Rules
        • Thank you very much!!! But still, somehow you can refer to Array [j] as a pointer? - Sergey041691
        • @Rules, damn it, is it really not clear that it was a typo? --- @ Sergey041691, do you like crutches and like "hemorrhoids"? In your case, it would be logical to ask for help from the standard template library. - Salivan
        • I would love to, but I do not know how to use STL, I have not read it. - Sergey041691
        • @ Sergey041691, nothing, you will soon understand how this will play into your hands) - Salivan

        The most trump method is to use boost. It has standard pointer storage containers that are compatible with the sort function.

         #include <iostream> using namespace std; #include <boost/ptr_container/ptr_vector.hpp> #include <boost/bind.hpp> using namespace boost; struct class1 { int value; class1(int v) : value(v) { } }; int main() { ptr_vector<class1> arr; arr.push_back(new class1(5)); arr.push_back(new class1(4)); arr.push_back(new class1(3)); arr.push_back(new class1(2)); arr.push_back(new class1(1)); sort(arr.begin(), arr.end(), bind(&class1::value, _1) < bind(&class1::value, _2)); for (auto el : arr) cout << el.value; cout << endl; return 0; }