C++ insert struct set

复制代码
//Model/BookStruct.cpp

#include <iostream>

using namespace std;

struct BookStruct
{
    int BookIndex;
    long double BookId;
    char *BookName;
    char *BookTitle; 
    bool operator < (const BookStruct &other) const 
    { 
        return BookIndex < other.BookIndex; 
    }
};
复制代码
//Model/Util.h
#ifndef Util_H
#define Util_H

#include <chrono>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <typeinfo>
#include <thread>
#include <unistd.h>
#include <uuid/uuid.h>
#include <vector>

#include "Model/BookStruct.cpp"

using namespace std;

class Util
{
public:
    static char *dtVlaue;
    static char *uuidValue;
    Util();
    ~Util();
    void structSet31(int len);
    void printStructSet30(set<BookStruct> &st);
    void getStructSet29(set<BookStruct> &st,int len);
    char *getTimeNow();
    char *getUuid();
    
};
#endif
复制代码

复制代码
//Util.cpp
#include "Model/Util.h"

char *Util::dtVlaue = (char *)malloc(30);
char *Util::uuidValue = (char *)malloc(40);


void Util::structSet31(int len)
{
    set<BookStruct> st;
    getStructSet29(std::ref(st), len);
    printStructSet30(std::ref(st));
    cout << getTimeNow() << ",finished in void Util::structSet31(int len)!!!" << endl;
}

void Util::printStructSet30(set<BookStruct> &st)
{
    set<BookStruct>::iterator itr = st.begin();
    while (itr != st.end())
    {
        cout << fixed << "Index=" << itr->BookIndex << ",Id=" << itr->BookId 
        << ",Name=" << itr->BookName << ",Title=" << itr->BookTitle << endl;
        free(itr->BookName);
        free(itr->BookTitle);
        itr++;
    }

    cout << getTimeNow() << ",finished in void Util::printStructSet30(set<BookStruct> &st)!" << endl
         << endl;
}

void Util::getStructSet29(set<BookStruct> &st, int len)
{
    for (int i = 0; i < len; i++)
    {
        BookStruct bs;
        bs.BookIndex = i;
        bs.BookId = (long double)i * i * i * i * i * i * i * i * i * i;
        bs.BookName = (char *)malloc(40);
        bs.BookTitle = (char *)malloc(40);
        strcpy(bs.BookName, getUuid());
        strcpy(bs.BookTitle, getUuid());
        st.insert(bs);
    }
}


char *Util::getTimeNow()
{
    time_t rawTime = time(NULL);
    tm tmInfo = *localtime(&rawTime);
    strftime(dtVlaue, 20, "%Y%m%d%H%M%S", &tmInfo);
    return dtVlaue;
}

char *Util::getUuid()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
}
复制代码

 

 
复制代码

 

复制代码
//main.cpp
#include "Model/Util.h"

void structSet18(int len);

int main(int args, char **argv)
{
    try
    {
        structSet18(atoi(argv[1]));
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}

void structSet18(int len)
{
    Util ul;
    ul.structSet31(len);
}
复制代码

 

2.Compile via g++

g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -lmysqlclient -luuid -lpthread;

3.Run

time ./h1 10000000;

 

 

 

Be cautious,the key located at implement operator < explicit  and its const modifier in BookStruct.cpp file as below

 

bool operator < (const BookStruct &other) const 
    { 
        return BookIndex < other.BookIndex; 
    }

 

If I did't add the above operator < implementation method,when compile it will throw exceptions as below snapshot.

/usr/include/c++/9/bits/stl_function.h:386:20: error: no match foroperator<’ (operand types are ‘const BookStruct’ and ‘const BookStruct’)

 

posted @   FredGrit  阅读(96)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2020-06-03 C# list all cultureinfos
2016-06-03 Linq
2016-06-03 var
2016-06-03 checked,unchecked
2016-06-03 StringBuilder.sb.AppendLine();
2016-06-03 \" 转义字符, \a系统警报,逐字字符串(verbatim string)
2016-06-03 BigInteger
点击右上角即可分享
微信分享提示