Convert uint64_t to std::string

后端 未结 4 1091
时光说笑
时光说笑 2021-02-13 23:42

How can I transfer uint64_t value to std::string? I need to construct the std::string containing this value For example something like this:

void genString(uint6         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 23:45

    I use something like this code below. Because it's a template it will work with any type the supports operator<< to a stream.

    #include 
    
    template 
    std::string tostring(const T& t)
    {
        std::ostringstream ss;
        ss << t;
        return ss.str();
    }
    

    for example

    uint64_t data = 123;
    std::string mystring = tostring(data);
    

提交回复
热议问题