Search code examples
c++tostringitoa

itoa replace to std::to_string how to


How can i replace this

    itoa(i, buf, 10);
    key1.append(buf);
    key2.append(buf);
    key3.append(buf);

with std::to_string

itoa was giving me an error it was not declared in scope that its not part of standard

I heard i can use std::to_string to make another version of it how do i do this?


Solution

  • If you use C++11, you can use std::to_string

    #include <string>
    std::string buf = std::to_string(i);
    key1.append(buf);
    key2.append(buf);
    key3.append(buf);
    

    or you could just use std:stringstream

    #include <sstream>
    std::stringstream ss;
    ss << i;
    key1.append(ss.str());
    key2.append(ss.str());
    key3.append(ss.str());