• Easiest way to convert int to string in C++

Easiest
way to convert int to string in C++

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

1.
int a = 10;
char *intStr = itoa(a);
string str = string(intStr);

2.
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

C++0x introduces std::stoi (and
variants for each numeric type) and std::to_string,
the counterparts of the C atoi and itoa but
expressed in term of std::string.
std::string s = std::to_string(42);

is therefore the shortest way I can think of.

Note: see [string.conversions] (21.5 in
n3242)
http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c

上文漏洞颇多，更好的参见帖子http://www.cnblogs.com/nzbbody/p/3504199.html

int本身也要用一串字符表示，前后没有双引号，告诉编译器把它当作一个数解释。缺省情况下，是当成10进制（dec）来解释，如果想用8进制，16进制，怎么办？加上前缀，告诉编译器按照不同进制去解释。8进制（oct）---前缀加0，16进制（hex）---前缀加0x或者0X。

string前后加上双引号，告诉编译器把它当成一串字符来解释。

注意：对于字符，需要区分字符和字符表示的数值。比如：char a = 8；char b = '8'，a表示第8个字符，b表示字符8，是第56个字符。

int转化为string

1、使用itoa（int to string）
//char *itoa( int value, char *string,int radix);
// 原型说明：
// value：欲转换的数据。
// string：目标字符串的地址。
// 返回指向string这个字符串的指针

int aa = 30;
char c[8];
itoa(aa,c,16);
cout<<c<<endl; // 1e注意：itoa并不是一个标准的C函数，它是Windows特有的，如果要写跨平台的程序，请用sprintf。

展开全文
• ## A wayto learn

热门讨论 2017-01-23 19:54:12
There was a guy , who named IS .... He wanted to be a rock God ! But he had a problem . En,there always has a problem . What's his problem? His problem was he can't playguitar right n


There was a guy , who named IS . IS had a dream . He wanted  to be a rock God ! But he had a problem . En,there always has a problem . What's his problem? His problem was he can't playguitar
right now. He can't play the guitar , he couldn't play the guitar , noguitar for IS. That's a little problem.
So hedecided to find  the best guitar teacherin the world . Did he find the best guitar teacher in the Mexico? No ,not inMexico , In the world . So he went to AJ . Why did he find AJ?
Cause AJ is thebest guitar teacher in the world . Everybody knows that . IS said to AJ"Teach me how to play the guitar ! I what to be a rock God !" AJ saidto him " You must imitate me ! Dress like me ! Move like me ! Yell like me! Look cool like me !" IK asked
"Don't I have to practiceguitar?" Did IK think he need to practice guitar ? Yes , He asked"Don't I have to practice guitar?"

OK ,That's all ,  I don't intend to tell you all this story, do you remember this story ?
If you think it's boring to learn English , find some interesting thing , chang your way ! For example , drew something  !


展开全文
• A Smarter Way to Learn JavaScript 亚马逊上面评价超高的一本书，我也是找了好久才找到。 包括 pdf, epub, mobi 三个版本
• Today when I read my co-worker's codes, I found an interesting way to validate if a string is numeric. Here is my way to do. Because I'm not good at regex, every time I need to search online and fin
Today when I read my co-worker's codes, I found an interesting way to validate if a string is numeric.
Here is my way to do.  Because I'm not good at regex, every time I need to search online and find a solution.
public static boolean isNumeric(String s) {
return java.util.regex.Pattern.matches("\\d+", s);
}
Here is my co-worker's code. It's really smart, right? I have never thought I can do in this way.
 protected boolean isNumber(String fieldValue){
boolean isValid = true;
try{
Integer field = Integer.parseInt(fieldValue);
if(field <= 0){
isValid = false;
}
}catch(NumberFormatException e){
isValid = false;
}
return isValid;

}

展开全文
• One effective way to reduce lock contention is to create a hierarchy
One effective way to reduce lock contention is to create a hierarchy
展开全文
• 最近读完了《Go入门指南》，这本书理论和实践结合，是学习golang的很好的学习资料。 书中的例子，是很好的参考。适合初学者入门以及有一定基础的人进阶，读来定有...电子书: The way to go github:The way to go ...
• reference:  http://www.geeksforgeeks.org/efficient-way-to-multiply-with-7/ ...Figure out a way to multiply with 7 efficiently. Solution: We can multiply a number by
• 曾几何时，觉得on one’s way to do特别地顺，后来发现多本词典上赫然列着on one’s way to doing的词条，于是顿悟：to是介词，一定要接doing。可是近来又发现，某些on one’s way后加to doing极为别扭。当然，仅凭...
• There's plenty of information out there about how to write code.Here's some advice on how to delete code. The best way to delete code This may seem obvious, but I guess it isn't, because of the vari
• ## The best wayto improve your listening

千次阅读 热门讨论 2017-09-16 09:33:20
What's the best way to improve your listening? In this video, I'm going share with you the technique.I use to my listening.This technique works.This technique works.And I wish I had known this tech
• And changing the way you speak to yourself will change the way you feel about yourself. Act the way Dorit did with the children: Don't reward negative behavior with attention by lingering on your ...
• way to visualize mip levels Recently a discussion on Twitter about folks using 2048 textures on a pair of dice spawned this post. How do artists know if the textures are too high or too low
• If your data is a hierarchy, a treemap is a good way to show all the values at once and keep the structure in the visual. This is a quick way to make a treemap in R.
• http://www.brendangregg.com/blog/2014-05-09/xen-feature-detection.html http://www.dmo.ca/blog/detecting-virtualization-on-linux/ http://unix.stackexchange.com/questions/89714/easy-way-to-determine
• <br />Strange Way to Express IntegersTime Limit: 1000MS Memory Limit: 131072KTotal Submissions: 3301 Accepted: 810 Description   Elina is reading a book written by Rujia Liu, which...
• 在黑客的世界里，当你拋出一个技术问题时，最终是否能得到有用的回答，往往取决于你所提问和追问的方式。本指南将教你如何正确的提问以获得你满意的答案。How To Ask Questions The Smart Way...
• Our project use Oracle Store procedure heavily, and due to its read more write less trait, one SP involves several SQLs which load data from different tables separately for the sake of data retrieving
• Simple way to export SQL Server data to Text FilesWritten By: Ken Simmons -- 11/24/2008 -- 2 comments Stay informed - get the MSSQLTips.com newsletter and win - click here
• Is there a way to import multiple vCards to Gmail contacts manager!
• Fastest way to estimate rows in a table A friend wrote to me recently with a question. He was working on a method to ship application metrics to statsd on a 1 minute interval. He had three ...
• ## Easy wayto backup redmine

千次阅读 2013-10-22 15:00:42
I use this to install redmine: http://bitnami.com/stack/redmine It is bitnami-redmine. The uploaded files are here: BitNami\redmine-2.3.2-0\apps\redmine\htdocs\files The my sql ...
• 通过smarty模板引擎把php数组传递给javascript... 这是原来我总结的解决办法： javascript调用smarty变量的方法 ...http://stackoverflow.com/questions/6857468/a-better-way-to-convert-js-object-to-array
• 上一篇博客《Go语言学习之变量(The way to go)介绍了go中的变量，今天就介绍常量。const关键字跟c++中一样，go中同样具有const关键字，来声明常量。 语法是这样的：const variable type = value;下面就是简单的常量...
• In computer science and information science, ontologies are used to formally represent knowledge within a domain . An ontology is defined as a formal, explicit specification of a shared ...
• Even if Im not paticilary interested in rootkit developingfiled, I visit www.rootkit.com from time to time to check if thereis any new idea regarding rootkits, not because I need it to developthem, b
• A Quick Way to Copy DataRowInstead of copying DataRow column by column, the following code copies data in one line from the source to the destination row:DataTable dtDest = new DataTable();dtD
• Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choosekdifferent positive integersa1...
• Easiest Way to Run CUDA on Visual Studio 2008

...