首页 > 其他分享 >cpp class constructor initialize list and override cout

cpp class constructor initialize list and override cout

时间:2023-07-14 22:56:46浏览次数:30  
标签:std isbn string time list value constructor include cout

//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

相关文章

  • Python - list VS tuple, list() VS []
    差异一:list可变vstuple不可变列表是动态的,长度大小不固定,可以随意地增加、删减或者改变元素(mutable)。而元组是静态的,长度大小固定,无法增加删减或者改变(immutable)。#Jupyter格式tup=(1,2,3,4)new_tup=tup+(5,)#创建新的元组new_tup,并依次填充原元组的值new_......
  • dede织梦标签,dede:arclist用法与详解
    标签名称:arclist标记简介:织梦常用标记,也称为自由列表标记,其中imglist、imginfolist、specart、coolart、autolist都是由该标记所定义的不同属性延伸出来的别名标记。功能说明:获取指定文档列表适用范围:全局使用基本语法:{dede:arclist?flag='h'typeid=''row=''col=''titlelen=......
  • java获取list类类型
    Java获取List类类型在Java中,要获取List的类类型可以通过以下步骤来实现。在本文中,我将详细介绍每个步骤以及使用的代码。步骤步骤描述步骤1创建一个List对象步骤2获取List对象的类类型步骤1:创建一个List对象首先,我们需要创建一个List对象,我们可以使用ArrayLi......
  • java获取list的type
    Java获取List的Type在Java中,List是一种常用的数据结构,用于存储一组有序的元素。有时候我们需要获取List中元素的类型,以便进行一些操作或判断。本文将介绍几种获取List类型的方法,并提供相应的代码示例。方法一:通过泛型参数获取类型在Java中,我们可以使用泛型来定义List的类型。通......
  • C#移除List中特定元素
    在List里面移除其中一个元素之后,原有的索引以及索引对应的值会发生改变,如果按照原有的索引值删除,就会误删除其它元素。1.实现思路原始List为A,将需要删除的元素放到一个ListB里面,遍历此List,A再使用Remove依次移除元素。下面代码实现的是移除索引为前两位的元素2.代码 usin......
  • freemarker取list前几条数据
    freemarker取list前几条数据 1、取前几条数据<@_categoryListid=4><#assignn=0/>//list外先定义assign<#listpage.listascolumn><#assignn=n+1/>//list里面再定义assign<#ifngt3><#br......
  • 注解 @RequiredArgsConstructor
    注解@RequiredArgsConstructor生成带有必需参数的构造函数。必需的参数是最终字段和具有约束的字段,例如@NonNull。完整的文档可在@lconstructor的项目lombok功能页面上找到。即使未列出,该注释也具有onConstructor参数。有关更多详细信息,请参见完整的文档。这个是基于lombo......
  • kubernetes 实现 list-watch 的底层原理
    我们都知道,controller-manager,scheduler,kubelet会向apiserver监听感兴趣的对象,当监听对象的内容或状态发生变化后,对应的事件会立即推送到监听者。借由这套事件通知机制,kubernetes才能良好地运转。那么这套事件通知机制是如何实现并驱动的呢?1.etcd在k8s中,apiserver是......
  • MyBatis返回resultType=Map的用法, 返回List<Map<String,String>>
    <selectid="statOnlineAndNotlineNumber"resultType="java.util.Map"parameterType="java.lang.String">SELECTonline_stateasstate,COUNT(online_state)asnumberFROMwl_rm_t_vehicle_state<iftest="operatorCode!=nu......
  • C#使用泛型方法将Datatable转换成List对象集合
     在项目中遇到需要将Datatable转换成对象的需求,通过dr[0]取下标这种获取,如果数据的顺序发生了改变则需要改变全部,工作量大foreach(DataRowdrindt.Rows){CheckDetailinfo=newCheckDetail();info.org_id=dr[0].ToStrin......