如何将数字(包括double型)转换为字符串 (转载)
今天跟一个在腾讯工作的同学聊天了,他问我如何将一个数转换为一个字符串,我跟他说是这样的:
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
void main()
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
//预处理时将会产生如下代码。
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
void main()
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
运行结果:
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
In quotes in the printf function call
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
"In quotes when printed to the screen"
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
"This: \" prints an escaped double quotation mark"
![](http://panpan.blog.51cto.com/images/editer/InBlock.gif)
关于#的用法还有很多,希望有兴趣的读者能够留言,我们一起讨论。
本文出自 “C++技术” 博客,请务必保留此出处http://panpan.blog.51cto.com/489034/102813
实践:
#define toString(x) (#x)
#define stringer(x) (printf(#x"\n"))
#define mynameandbirthday(x) ("hekexin"#x)
int main()
{
std::string str(toString(1000));
std::cout<<str<<std::endl;
stringer(hello world);
stringer(1234fff5455);
std::cout<<mynameandbirthday(19841102)<<std::endl;
std::cout<<mynameandbirthday(19841223)<<std::endl;
return 0;
}