• C++ list 设计增删改查4个接口


    list.cpp

    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <list>
    
    typedef struct item{
        char id[64];
        char ip[64];
        int port;
        int status;
        bool operator==(struct item b) const  
        {  
            return (strcmp(this->id, b.id)==0);  
        }  
        bool operator!=(struct item b) const  
        {  
            return (strcmp(this->id, b.id)!=0);
        }  
    } item_t;
    
    template < typename T>
    class db_t { 
      public:
        std::list<T> l;
      public:
        int insert(T t)
        {
            auto it = std::find(l.begin(), l.end(), t);
            if (it == l.end()) {
                l.insert(it, t);
                return 0;
            }else{
                return -1;
            }
        }
    
        int remove(const char* id){
            int i;
            if(l.size() == 0) return -1;
            
            for (T &n : l) {
                if(strcmp(n.id, id) == 0){
                    l.remove(n);
                    break;
                }else i++;
            }
            if(i == l.size()) return -1;
            else return 0;
        }
    
        int update(T t, const char* id){
            int i;
            if(l.size() == 0) return -1;
    
            for (T &n : l) {
                if(strcmp(n.id, id) == 0){
                    n = t;
                    break;
                }else i++;
            }
            if(i == l.size()) return -1;
            else return 0;        
        }
    
        T select(const char* id){
            T t;
            memset(&t, 0, sizeof(T));
            for (T &n : l) {
                if(strcmp(n.id, id) == 0){
                    t = n;
                    break;
                }
            }
            return t;
        }
    
        int size()
        {
            return l.size();
        }
    
        void dump(){
            for (T &n : l) {
                std::cout << n.id << '
    ';
                std::cout << n.ip << '
    ';
                std::cout << n.port << '
    ';
                std::cout << n.status << '
    ';
            }    
        }
    
      private:
    
    };
    
    int main()
    {
        db_t<item_t> db;
        
        item_t item1;
        memset(&item1, 0, sizeof(item_t));
        strcpy(item1.id,"34020000001320000001"); 
    
        item_t item2;
        memset(&item2, 0, sizeof(item_t));
        strcpy(item2.id,"34020000001320000002");
    
        item_t item3;
        memset(&item3, 0, sizeof(item_t));
        strcpy(item3.id,"34020000001320000003"); 
        strcpy(item3.ip,"127.0.0.1");
     
        std::cout << "-------------insert----------------" << '
    ';
    
        //insert
        db.insert(item1);
        db.insert(item2);
        db.insert(item3);
    
        std::cout << db.size() << '
    ';
        db.dump();
        
        std::cout << "-------------delete----------------" << '
    ';
    
        //delete
        db.remove("34020000001320000001");
        db.remove("34020000001320000002");
        //db.remove("34020000001320000003");
    
        std::cout << db.size() << '
    ';
        db.dump();
        
        std::cout << "-------------select----------------" << '
    ';
    
        //select
        item_t item = db.select("34020000001320000003");
        std::cout << item.ip << '
    ';
    
        std::cout << db.size() << '
    ';
        db.dump();
    
        std::cout << "-------------update----------------" << '
    ';
    
        //update
        item_t item_tmp = db.select("34020000001320000003");
        strcpy(item_tmp.ip,"0.0.0.0");
        item_tmp.port = 6000;
        item_tmp.status = 0;
        db.update(item_tmp, "34020000001320000003");
    
        std::cout << db.size() << '
    ';
        db.dump();
    
        return 0; 
    }

    build.sh

    g++ -o list list.cpp -std=c++11

    dong@ubuntu:~/C++/list$ ./list
    -------------insert----------------
    3
    34020000001320000001

    0
    0
    34020000001320000002

    0
    0
    34020000001320000003
    127.0.0.1
    0
    0
    -------------delete----------------
    1
    34020000001320000003
    127.0.0.1
    0
    0
    -------------select----------------
    127.0.0.1
    1
    34020000001320000003
    127.0.0.1
    0
    0
    -------------update----------------
    1
    34020000001320000003
    0.0.0.0
    6000
    0
    dong@ubuntu:~/C++/list$

    c++ list 模拟的简易内存数据库,几十行代码就能实现个最小的存储系统了。

  • 相关阅读:
    日志服务器 03-部署日志服务器(网络设备部分)
    日志服务器 02-部署日志服务器(主机部分)
    日志服务器 01-Linux日志系统syslog
    nyoj--523--亡命逃窜(BFS水题)
    hdoj--3488--Tour(KM)
    hdoj--1533--Going Home(KM)
    hdoj--2255--奔小康赚大钱(KM算法模板)
    LightOJ--1152--Hiding Gold(二分图奇偶建图)(好题)
    hdoj--5093--Battle ships(二分图经典建图)
    LightOJ--1149--Factors and Multiples(二分图好题)
  • 原文地址:https://www.cnblogs.com/dong1/p/11119699.html
Copyright © 2020-2023  润新知