Question

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?

Was it helpful?

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());

OTHER TIPS

To use itoa() function, you need to include stdlib.h file, then you will not see the error while using itoa().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top