本文介绍了typecast int to string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我想将int类型转换为std :: string我该怎么办呢。 这是样本代码。 int NewList [500]; //用整数值填充NewList。 。 ...... ....... //用指定位置的新列表值替换文件内容。 /> int i = 0; std :: string line; ifstream inFile(sample); //打开一个文件来读取 while(getline(inFile,line)) { int comma1Pos = line.find('',''' ); int comma2Pos = line.find('','',comma1Pos + 1); int numChars = comma2Pos - comma1Pos - 1; line.erase(comma1Pos + 1,numChars); // line.insert(comma1Pos + 1,(std :: string)NewList [i]); i ++; } inFile.close(); 在上面的代码中,函数insert将第二个参数作为一个字符串,所以我 正在尝试类型转换NewList [i]到字符串。 编译器抛出一个类型转换错误说类型cast int到string不是 可能。 我有什么方法可以输入相同的内容。 问候, VenkatHi All,I want to typecast int to std::string how can i do it.Here is the sample code.int NewList[500];//Fill the NewList with integers values...............//Replace the file contents with new list values at a specified location.int i=0;std::string line;ifstream inFile(sample);//opens a file to readwhile (getline (inFile, line) ){int comma1Pos = line.find('','');int comma2Pos = line.find('','', comma1Pos+1);int numChars = comma2Pos - comma1Pos - 1;line.erase(comma1Pos+1, numChars); //line.insert(comma1Pos+1, (std::string)NewList[i]);i++;}inFile.close();In the above code the function insert takes 2nd argument as a string so iwas trying to type cast NewList[i] to string.The complier throws a type cast error saying type cast int to string is notpossible.Is there any way i can type cast the same.regards,Venkat推荐答案 - 要获得我真实的电子邮件地址,删除两个onkas - Dipl.-通知。 Hendrik Belitz 中央电子学院 研究中心Juelich--To get my real email adress, remove the two onkas--Dipl.-Inform. Hendrik BelitzCentral Institute of ElectronicsResearch Center Juelich 不,那是不是铸造的内容。施法可以改变一些东西到相关的东西。虽然对于人类整数和他们的 字符串表示可能是相关的,但对于计算机来说它们是非常不同的。 作为另外,你不应该在C ++中使用C风格的演员表,C ++有更多的演员阵容:static_cast<>,dynamic_cast<>,const_cast<>和 reinterpret_cast<>。熟悉这些并且再也不使用 C风格的演员。它会为你节省很多的悲伤。 所以问题现在变成了,如何将数字转换为字符串。你有多少种方法可以做到这一点,最容易和最多的C ++是: #include< sstream> std :: string toString(int i) { std :: stringstream s; s<< i; 返回s.c_str(); } 好的,这样可行,但也许你想用这个对于无符号的整数。 或多头。您可以多次创建相同的函数, 重载参数: std :: string toString(long){...} std :: string toString(unsigned int){...} std :: string toString(unsigned long){...} std :: string toString(float){...} std :: string toString(double){...} 幸运的是,有一个更方便。我们可以通过使用模板的魔力让编译器为 我们做这件事: 模板< typename T> std :: string toString(T t) { std :: stringstream s; s<< t; 返回s.c_str(); } 这将使编译器生成所有上述函数 自动,但只有我们实际使用的! (请注意,在使用它之前,编译器必须看到此代码 ,您不能只使用 原型并在另一个C ++中定义该函数所以这通常是 进入一些标题。) 所以如果你使用toString(i),其中i是一个整数,编译器会 用T代替int,最后得到与上面相同的结果。但是如果 我们使用toString(l),其中l是long,编译器会自动生成 生成上述内容很长时间。模板可以非常强大! HTH, M4No, that is not what casting is about. Casting can change something tosomething related. Although for humans integers and theirstring-representations may be related, for computers they are verydifferent.As a side note, you should never use C-style casts in C++, C++ has muchbetter casts: static_cast<>, dynamic_cast<>, const_cast<> andreinterpret_cast<>. Familiarize yourself with those and never use theC-style casts again. It will save you a lot of grief.So the question now becomes, how to convert a number to a string. Thee area number of ways to do so, the easiest and most C++ish:#include <sstream>std::string toString(int i){std::stringstream s;s << i;return s.c_str();}OK, this works, but maybe you want to use this for unsigned ints as well.Or for longs. You could create the same function multiple times,overloading on the argument:std::string toString(long) { ... }std::string toString(unsigned int) { ... }std::string toString(unsigned long) { ... }std::string toString(float) { ... }std::string toString(double) { ... }Fortunately, there is an easier way. We can get the compiler to do it forus by using the magic of templates:template<typename T>std::string toString(T t){std::stringstream s;s << t;return s.c_str();}This will make the compiler produce all of the above functionsautomagically, but only the ones we actually use! (Do note that this codemust be ''seen'' by the compiler before you use it, you cannot just use aprototype and define the function in another C++ file. So this typicallygoes in some header.)So if you use toString(i), where i is an integer, the compiler willsubstitute int for T and we end up with exectly the same as above. But ifwe use toString(l), where l is a long, the compiler automagicallygenerates the above for a long. Templates can be so incredibly powerful!HTH,M4 这篇关于typecast int to string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 17:51