std::sort 不是排序

问题描述 投票:0回答:1

我想修改 xmrig 代理源以打印按字母顺序排序的工作人员。

在以下方法中添加两行代码 但不起作用。

auto pr_workers=m_workers;
    std::sort(pr_workers.begin(), pr_workers.end(), [](const Worker a, const Worker b) {
        return a.name() < b.name();
    });

void xmrig::Workers::printWorkers()
{
    if (!isEnabled()) {
        LOG_ERR("Per worker statistics disabled");

        return;
    }

    char workerName[24] = { 0 };
    size_t size = 0;

    Log::print(WHITE_BOLD_S "%-23s | %-15s | %-5s | %-8s | %-3s | %11s | %11s |",
               "WORKER NAME", "LAST IP", "COUNT", "ACCEPTED", "REJ", "10 MINUTES", "24 HOURS");

    auto pr_workers=m_workers;
    std::sort(pr_workers.begin(), pr_workers.end(), [](const Worker a, const Worker b) {
        return a.name() < b.name();
    });

    for (const Worker &worker : pr_workers) {
        const char *name = worker.name();
        size = strlen(name);

        if (size > sizeof(workerName) - 1) {
            if (m_mode == Agent) {
                memcpy(workerName, name, 20);
                memcpy(workerName + 20, "...", 3);
            }
            else {
                memcpy(workerName, name, 6);
                memcpy(workerName + 6, "...", 3);
                memcpy(workerName + 9, name + size - sizeof(workerName) + 10, sizeof(workerName) - 10);
            }
        }
        else {
            strncpy(workerName, name, sizeof(workerName) - 1);
        }

        Log::print("%-23s | %-15s | %5" PRIu64 " | %8" PRIu64 " | %3" PRIu64 " | %6.2f kH/s | %6.2f kH/s |",
                   workerName, worker.ip(), worker.connections(), worker.accepted(), worker.rejected(), worker.hashrate(600), worker.hashrate(86400));
    }
}

c++ sorting
1个回答
0
投票

由于

name()
返回
const char*
,因此您需要使用
std::strcmp
进行字典顺序比较。如果您这样做
return a.name() < b.name();
,您将比较两个指针,这不会在这种情况下产生任何有意义的结果。

return std::strcmp(a.name(), b.name()) < 0;
© www.soinside.com 2019 - 2024. All rights reserved.