C++ insert struct set

2022/6/4 1:20:14

本文主要是介绍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 for ‘operator<’ (operand types are ‘const BookStruct’ and ‘const BookStruct’)

 



这篇关于C++ insert struct set的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程