C++常用基础函数整理

 一、C++适应于任何内置类型数据转换的函数

函数定义如下,这里用到了模板。

template <class FromType, class ToType>
inline ToType To(const FromType& from, const ToType& init_to_value)
{
    ToType to = init_to_value;
    std::stringstream ss;
    ss << from;
    ss >> to;
    return to;
}

使用如下:

To<uint64_t, std::string>(kfuin, "");  #uint64数值转换为string字串
To<std::string, uint64_t>(strkfuin,0); #string字串转换为uint64数值

To<std::string, uint32_t>(flagNum, 0); #string字串转换为uint64数值

关于格式转换也不要纠结:

C++ 数值与 string 的相互转换_Dablelv的博客专栏-CSDN博客

二、根据时间戳获取可读日期(时间)的函数

1、将秒时间戳转换成day日期

std::string GetDateStrFromStamp(time_t time)
{
    struct tm 

版权声明:本文为mijichui2153原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。