I know it sounds stupid, but I'm using MinGW32 on Windows7, and "to_string
was not declared in this scope." It's an actual GCC Bug, and I've followed these instructions and they did not work. So, how can I convert an int to a string in C++11 without using to_string
or stoi
? (Also, I have the -std=c++11
flag enabled).
Asked
Active
Viewed 8,077 times
6

user703016
- 37,307
- 8
- 87
- 112

Ericson Willians
- 7,606
- 11
- 63
- 114
-
5Well, you could use MinGW-w64 instead which supports it – M.M Jun 22 '15 at 07:16
-
2You could use `sprintf`. Actually there are a *lot* of options - `atoi` and company (not-quite-standard), string streams, the printf functions. – Panagiotis Kanavos Jun 22 '15 at 07:18
-
@MattMcNabb the question is about formatting ints to strings, not parsing strings to ints – Panagiotis Kanavos Jun 22 '15 at 07:20
-
3`to_string` and `stoi` are the inverse of each other. Which is it you want to do? – kfsone Jun 22 '15 at 07:20
-
@PanagiotisKanavos why did you mention `atoi` then – M.M Jun 22 '15 at 07:25
-
He probably meant `itoa` – Jay Bosamiya Jun 22 '15 at 07:26
5 Answers
9
Its not the fastest method but you can do this:
#include <string>
#include <sstream>
#include <iostream>
template<typename ValueType>
std::string stringulate(ValueType v)
{
std::ostringstream oss;
oss << v;
return oss.str();
}
int main()
{
std::cout << ("string value: " + stringulate(5.98)) << '\n';
}

Galik
- 47,303
- 4
- 80
- 117
4
I'd like to answer it differently: just get mingw-w64.
Seriously, MinGW32 is just so full of issues it's not even funny:
- http://sourceforge.net/p/mingw/bugs/1578/
- http://sourceforge.net/p/mingw/mailman/message/23108552/
- wWinmain, Unicode, and Mingw
With MinGW-w64 you get for free:
- support for Windows Unicode entry point (
wmain
/wWinMain
) - better C99 support
- better C++11 support (as you see in your question!)
- large file support
- support for C++11 threads
- support for Windows 64 bit
- cross compiling! so you can work on your Windows app on your favorite platform.

rr-
- 14,303
- 6
- 45
- 67
-
-
Ok, you've convinced me haha.. I'll uninstall it and download mingw-w64. – Ericson Willians Jun 22 '15 at 08:19
2
You can use stringstream
.
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main() {
int num = 12345;
stringstream ss;
ss << num;
string str;
ss >> str;
cout << str << endl;
return 0;
}

Ziming Song
- 1,176
- 1
- 9
- 22
1
You could roll your own function to do it.
std::string convert_int_to_string (int x) {
if ( x < 0 )
return std::string("-") + convert_int_to_string(-x);
if ( x < 10 )
return std::string(1, x + '0');
return convert_int_to_string(x/10) + convert_int_to_string(x%10);
}

Jay Bosamiya
- 3,011
- 2
- 14
- 33
-
2"You could" != "You should". Just pointing out one other possible thing. – Jay Bosamiya Jun 22 '15 at 07:24
1
Despite the fact that previous answers are better I want to give you another possibility to implement an INT to STRING method following the next old school code:
#include <string>
std::string int2string(int value) {
char buffer[20]; // Max num of digits for 64 bit number
sprintf(buffer,"%d", value);
return std::string(buffer);
}

omotto
- 1,721
- 19
- 20
-
-
Nop, beacuse 64bit needs 19 digits (in the example there are 20 digit buffer) 9,223,372,036,854,775,807 (19 digits) – omotto Feb 25 '18 at 20:09
-
-
-