//book.h
#pragma once #include <iostream> class book { public: int idx; std::uint64_t id; std::string author; std::string content; std::string comment; std::string isbn; std::string summary; std::string topic; book(int idx_vlaue, std::uint64_t id_value, std::string author_value, std::string content_value, std::string isbn_value, std::string summary_value, std::string topic_value) : idx(idx_vlaue), id(id_value), author(author_value), content(content_value), isbn(isbn_value), summary(summary_value), topic(topic_value) { } friend std::ostream &operator<<(std::ostream &os, const book &bk); }; std::ostream &operator<<(std::ostream &os, const book &bk) { os<<"Idx:"<<bk.idx<<",id:"<<bk.id<<",author:"<<bk.author<<","<<bk.content<<",comment:"<<bk.comment<<",isbn:" <<bk.isbn<<",summary:"<<bk.summary<<",topic:"<<bk.topic<<std::endl; return os; }
The critical part of override cout is
//declare override method cout as ostream friend method in class friend std::ostream &operator<<(std::ostream &os, const book &bk); //implement the overriden friendly method std::ostream &operator<<(std::ostream &os, const book &bk) { os<<"Idx:"<<bk.idx<<",id:"<<bk.id<<",author:"<<bk.author<<","<<bk.content<<",comment:"<<bk.comment<<",isbn:" <<bk.isbn<<",summary:"<<bk.summary<<",topic:"<<bk.topic<<std::endl; return os; }
The whole code as below
//book.h #pragma once #include <iostream> class book { public: int idx; std::uint64_t id; std::string author; std::string content; std::string comment; std::string isbn; std::string summary; std::string topic; book(int idx_vlaue, std::uint64_t id_value, std::string author_value, std::string content_value, std::string isbn_value, std::string summary_value, std::string topic_value) : idx(idx_vlaue), id(id_value), author(author_value), content(content_value), isbn(isbn_value), summary(summary_value), topic(topic_value) { } friend std::ostream &operator<<(std::ostream &os, const book &bk); }; std::ostream &operator<<(std::ostream &os, const book &bk) { os<<"Idx:"<<bk.idx<<",id:"<<bk.id<<",author:"<<bk.author<<","<<bk.content<<",comment:"<<bk.comment<<",isbn:" <<bk.isbn<<",summary:"<<bk.summary<<",topic:"<<bk.topic<<std::endl; return os; } // main.cpp #include <algorithm> #include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <queue> #include <sstream> #include <thread> #include <uuid/uuid.h> #include <vector> #include "book.h" std::string get_time_now() { std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now(); time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now); struct tm tm_info = *localtime(&raw_time); auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()); auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()); auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()); std::stringstream ss; ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << "_" << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000) << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000) << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000); return ss.str(); } char *uuid_value = (char *)malloc(40); char *get_uuid_value() { uuid_t new_uuid; uuid_generate(new_uuid); uuid_unparse(new_uuid, uuid_value); return uuid_value; } void queue_demo(const int &len) { std::queue<book> que; for (int i = 0; i < len; i++) { que.emplace(i, static_cast<std::uint64_t>(i * i * i * i * i), get_uuid_value(), get_uuid_value(), get_uuid_value(), get_uuid_value(), get_uuid_value()); } std::cout << "queue size:" << que.size() << std::endl; while (!que.empty()) { book bk = que.front(); std::cout << bk; que.pop(); } std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl; } int main(int args, char **argv) { queue_demo(atoi(argv[1])); std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ",in " << __FUNCTION__ << std::endl; }
Compile
g++-12 -std=c++2a -I. *.cpp -o h1 -luuid -lpthread
Run
./h1 10
标签:std,isbn,string,time,list,value,constructor,include,cout From: https://www.cnblogs.com/Fred1987/p/17555184.html