C/C++ std::list 插入时即整理顺序(ASC升序)

下面是一个随机的 packet(帧)插入函数,ASC升序,按 "packet_seq" 来控制成员之间的顺序,该函数优化了,倾向头、倾向尾。

即:

根据 packet_seq 可以得出,插入位置的最短距离,更偏向左侧、还是右侧,该函数实现仅供参考,若需要多线程访问,需要确保 "代码临界区同步问题"。

cpp 复制代码
#include <iostream>
#include <list>
#include <memory>

struct tag_packet {
    uint32_t packet_seq;
};

void emplace_packet(std::list<tag_packet>& queue, const tag_packet& packet) {
    for (;;) {
        auto tail = queue.begin();
        if (tail == queue.end()) {
            queue.emplace_back(packet);
            break;
        }

        if (tail->packet_seq > packet.packet_seq) {
            queue.emplace_front(packet);
            break;
        }

        auto rtail = queue.rbegin();
        if (rtail->packet_seq <= packet.packet_seq) {
            queue.emplace_back(packet);
            break;
        }

        if ((packet.packet_seq - tail->packet_seq) > (rtail->packet_seq - packet.packet_seq)) {
            auto position = std::upper_bound(queue.rbegin(), queue.rend(), packet,
                [](const tag_packet& lhs, const tag_packet& rhs) noexcept {
                    return lhs.packet_seq > rhs.packet_seq;
                });

            queue.emplace(position.base(), packet);
        }
        else {
            auto position = std::lower_bound(queue.begin(), queue.end(), packet,
                [](const tag_packet& lhs, const tag_packet& rhs) noexcept {
                    return lhs.packet_seq < rhs.packet_seq;
                });

            queue.emplace(position, packet);
        }

        break;
    }
}

int main() {
    std::list<tag_packet> packets;
    tag_packet packet1 = { 100  };
    tag_packet packet2 = { 50, };
    tag_packet packet3 = { 200  };
    tag_packet packet4 = { 150  };
    tag_packet packet5 = { 75 };
    tag_packet packet6 = { 115 };
    tag_packet packet7 = { 45 };
    tag_packet packet8 = { 135 };
    tag_packet packet9 = { 195 };

    emplace_packet(packets, packet1);
    emplace_packet(packets, packet2);
    emplace_packet(packets, packet3);
    emplace_packet(packets, packet4);
    emplace_packet(packets, packet5);
    emplace_packet(packets, packet6);
    emplace_packet(packets, packet7);
    emplace_packet(packets, packet8);
    emplace_packet(packets, packet9);

    for (const auto& packet : packets) {
        std::cout << packet.packet_seq << std::endl;
    }

    return 0;
}
相关推荐
R-G-B15 小时前
【25】MFC入门到精通——MFC静态文本框 中字符串 连续输出 不覆盖先前的文本 换行输出
c++·mfc·mfc静态文本框输出字符串·mfc静态文本框连续输出字符串·mfc静态文本框换行输出字符串
FFZero118 小时前
【C++/Lua联合开发】 (二) Lua调用C++函数
c++·junit·lua
CoderCodingNo19 小时前
【GESP】C++四级真题 luogu-B4068 [GESP202412 四级] Recamán
开发语言·c++·算法
一个不知名程序员www19 小时前
算法学习入门---双指针(C++)
c++·算法
Maple_land19 小时前
常见Linux环境变量深度解析
linux·运维·服务器·c++·centos
Larry_Yanan19 小时前
QML学习笔记(四十四)QML与C++交互:对QML对象设置objectName
开发语言·c++·笔记·qt·学习·ui·交互
Want59519 小时前
C/C++大雪纷飞①
c语言·开发语言·c++
Mr_WangAndy20 小时前
C++设计模式_行为型模式_策略模式Strategy
c++·设计模式·策略模式·依赖倒置原则
LoveXming20 小时前
Chapter11—适配器模式
c++·设计模式·适配器模式·开闭原则
Benny_Tang20 小时前
题解:P7989 [USACO21DEC] Bracelet Crossings G
c++·算法