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;

C++ insert struct set

 

 

 

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’)

C++ insert struct set

 

原文链接: https://www.cnblogs.com/Fred1987/p/16340791.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ insert struct set

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401415

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:14
下一篇 2023年4月19日 上午9:15

相关推荐