Перейти из форума на сайт.

НовостиФайловые архивы
ПоискАктивные темыТоп лист
ПравилаКто в on-line?
Вход Забыли пароль? Первый раз на этом сайте? Регистрация
Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Вопросы по программированию на C/С++

Модерирует : ShIvADeSt

 Версия для печати • ПодписатьсяДобавить в закладки
На первую страницук этому сообщениюк последнему сообщению

Открыть новую тему     Написать ответ в эту тему

V0lt



Platinum Member
Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору

Код:
// chapter.h
#pragma once
 
#include <cstdint>
 
class Chapter
{
    static int counter;
    int order;
 
public:
    int64_t rt;
    Chapter(int64_t rt);
    Chapter& operator = (const Chapter& c);
 
    static int Compare(const void* a, const void* b);
    static bool Less(const Chapter& a, const Chapter& b);
};

Код:
// chapter.cpp
#include "chapter.h"
 
int Chapter::counter = 0;
 
Chapter::Chapter(int64_t rt)
{
    order = counter++;
    this->rt = rt;
}
 
Chapter& Chapter::operator = (const Chapter& c)
{
    if ( this != &c ) {
        order = c.counter;
        rt = c.rt;
    }
    return *this;
}
 
int Chapter::Compare(const void* a, const void* b)
{
    const Chapter* ca = static_cast<const Chapter*>(a);
    const Chapter* cb = static_cast<const Chapter*>(b);
 
    if (ca->rt > cb->rt) {
        return 1;
    } else if (ca->rt < cb->rt) {
        return -1;
    }
 
    return ca->order - cb->order;
}
 
bool Chapter::Less(const Chapter& a, const Chapter& b)
{
    return Compare(&a, &b) < 0;
}

Код:
// main.cpp
#include <algorithm>
#include <vector>
#include <stdio.h>
#include "chapter.h"
 
void main()
{
    std::vector<Chapter> chapters1 = {1000, 2000, 3000, 4000, 5000};
    std::vector<Chapter> chapters2 = {1000, 2000, 3000, 4000, 5000};
 
    qsort(chapters1.data(), chapters1.size(), sizeof(Chapter), Chapter::Compare);
 
    std::sort(chapters2.begin(), chapters2.end(), Chapter::Less);
 
    printf("\n");
    printf("\nPress any key to quit... ");
    getchar();
}

Всего записей: 10455 | Зарегистр. 05-02-2003 | Отправлено: 20:31 12-12-2017 | Исправлено: V0lt, 06:02 13-12-2017
Открыть новую тему     Написать ответ в эту тему

На первую страницук этому сообщениюк последнему сообщению

Компьютерный форум Ru.Board » Компьютеры » Прикладное программирование » Вопросы по программированию на C/С++


Реклама на форуме Ru.Board.

Powered by Ikonboard "v2.1.7b" © 2000 Ikonboard.com
Modified by Ru.B0ard
© Ru.B0ard 2000-2024

BitCoin: 1NGG1chHtUvrtEqjeerQCKDMUi6S6CG4iC

Рейтинг.ru