Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя red_coder

Автор red_coder, 13 лет назад, По-английски

suppose a class is as follows.. class cool { public: int a; int b; }; cool A[100];

now each element of array A[] has two sub elements 'a' and 'b'. Suppose we want to sort the array A[] in increasing order of 'a' and if two elements have equal 'a' then element with greater 'b' should be before. Can anyone give me the c++ code for the above purpose....

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
13 лет назад, # |
Rev. 2   Проголосовать: нравится +9 Проголосовать: не нравится

inline bool operator < (const cool& A, const cool& B) {return A.a < B.a || (A.a == B.A && A.b < B.b);}

And then you can simply write:

sort(A, A + 100);

  • »
    »
    13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    hey DimaPhil please tell me one thing , while coding the best sorting method that should be used is simple sort() function or any other

    • »
      »
      »
      13 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      I think yes, but I'm not sure.

      This method can also be useful:

      inline bool Less(cool A, cool B) { return A.a < B.a || (A.a == B.a && A.b < B.b); }

»
13 лет назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится
»
13 лет назад, # |
  Проголосовать: нравится +7 Проголосовать: не нравится
  • »
    »
    13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    thanks a lot DAIe , your code cleared my doubt. Can u please give me a link so that i can read more about these types of operations