替代投资()转换整串C++?[重复]
题
这个问题已经有一个答案在这里:
- 最简单的方式转换int串C++ 28答案
我想知道,如果有一个替代 itoa()
换一个整数字符串因为当我运行了它在visual Studio我得到警告,并且当我试试来建立我的计划在Linux下,我得到一个编辑错误。
解决方案
C++11您可以使用 std::to_string
:
#include <string>
std::string s = std::to_string(5);
如果你的工作与现有的C++11,可以使用C++流:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
采取从 http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
其他提示
提升::lexical_cast 工作得很好。
#include <boost/lexical_cast.hpp>
int main(int argc, char** argv) {
std::string foo = boost::lexical_cast<std::string>(argc);
}
考古学
投资是一个非标准的辅助功能设计以补充atoi标准的功能,并可能隐藏着一个sprintf(大多数功能可以实现的条款sprintf): http://www.cplusplus.com/reference/clibrary/cstdlib/itoa.html
C的方式
使用sprintf.或snprintf.或什么工具你找到。
尽管一些职能是不是在标准,因为正确地提到的"。参见样式"在一个他的意见,大多数编译器将提供一个替代(例如视觉C++有其自己的_snprintf你可以typedef到snprintf如果你需要的话)。
C++的方式。
使用C++流(在目前情况下std::stringstream(或甚至废弃std::strstream提议的草药萨特在他的一本书,因为它的有些快).
结论
你是用C++,这意味着你可以选择你想要的方式:
更快的方式(即C的方式),但是你应该是肯定的代码是一个瓶颈在应用程序(优化过早都是邪恶,等等。) 和你的代码是安全装封,以避免危险缓冲区的超支。
更安全的方式(即,C++的方法),如果你知道这个部分的代码不是至关重要的,以便更好地确保这一部分代码不会打破在随机的时刻,因为有人弄错了一个尺寸或指针(其中发生在真实的生活,就像...昨天,在我的电脑,因为有人认为,"酷"使用更快的方式没有真正需要它)。
尝试sprintf():
char str[12];
int num = 3;
sprintf(str, "%d", num); // str now contains "3"
sprintf()就像是printf(),但是输出到一串。
此外,作为我希望你喜欢它所提到的意见,可能需要使用snprintf()停止缓冲溢出发生(在那里数你换不合适的大小串。) 它的工作原理是这样的:
snprintf(str, sizeof(str), "%d", num);
幕后,lexical_cast不会这样的:
std::stringstream str;
str << myint;
std::string result;
str >> result;
如果你不想"拖在"提高对于这一点,那么用上述是一个好的解决方案。
我们可以界定我们自己 iota
功能用c++为:
string itoa(int a)
{
string ss=""; //create empty string
while(a)
{
int x=a%10;
a/=10;
char i='0';
i=i+x;
ss=i+ss; //append new character at the front of the string!
}
return ss;
}
不要忘记 #include <string>
.
С++11最后解决了这个提供 std::to_string
.还 boost::lexical_cast
为方便的工具,为老年编译器。
我使用这些模板
template <typename T> string toStr(T tmp)
{
ostringstream out;
out << tmp;
return out.str();
}
template <typename T> T strTo(string tmp)
{
T output;
istringstream in(tmp);
in >> output;
return output;
}
尝试 提升。格式 或 FastFormat, 高质量的C++库:
int i = 10;
std::string result;
与提升。格式
result = str(boost::format("%1%", i));
或FastFormat
fastformat::fmt(result, "{0}", i);
fastformat::write(result, i);
很明显,他们两个做了很多比一个简单的转换一个整数
实际上你可以换任何东西串的一个巧妙地编写模板功能。这些代码如使用一个循环创造的子目录在一个双赢的-32的系统。字符串连接的操作者、操作者+,被用来连接的一个根源与后生成的目录的名字。后缀是通过转换循环控制变量,i、C++串,使用的模板功能,并连接起来,另一串。
//Mark Renslow, Globe University, Minnesota School of Business, Utah Career College
//C++ instructor and Network Dean of Information Technology
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream> // string stream
#include <direct.h>
using namespace std;
string intToString(int x)
{
/**************************************/
/* This function is similar to itoa() */
/* "integer to alpha", a non-standard */
/* C language function. It takes an */
/* integer as input and as output, */
/* returns a C++ string. */
/* itoa() returned a C-string (null- */
/* terminated) */
/* This function is not needed because*/
/* the following template function */
/* does it all */
/**************************************/
string r;
stringstream s;
s << x;
r = s.str();
return r;
}
template <class T>
string toString( T argument)
{
/**************************************/
/* This template shows the power of */
/* C++ templates. This function will */
/* convert anything to a string! */
/* Precondition: */
/* operator<< is defined for type T */
/**************************************/
string r;
stringstream s;
s << argument;
r = s.str();
return r;
}
int main( )
{
string s;
cout << "What directory would you like me to make?";
cin >> s;
try
{
mkdir(s.c_str());
}
catch (exception& e)
{
cerr << e.what( ) << endl;
}
chdir(s.c_str());
//Using a loop and string concatenation to make several sub-directories
for(int i = 0; i < 10; i++)
{
s = "Dir_";
s = s + toString(i);
mkdir(s.c_str());
}
system("PAUSE");
return EXIT_SUCCESS;
}
分配一串足够的长度,然后使用snprintf.
最好的答案,国际海事组织,提供的功能在这里:
http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
它模仿的非ANSI功能提供的许多库。
char* itoa(int value, char* result, int base);
这也是闪电快速和优化以及下O3,原因你没有使用c++string_format()...或sprintf是,他们的速度太慢,对吗?
注意,所有的 stringstream
方法 可 涉及锁定使用的语言环境对象的格式。此 可 可以什么可担心的,如果你使用这种转换从多个线程...
在这里看到更多。 转换为数字符串与指定的长在C++
int number = 123;
stringstream = s;
s << number;
cout << ss.str() << endl;
如果你有兴趣在快以及安全的整串转换方法并不仅限于标准库,我可以推荐的 FormatInt
方法从 C++的格式 图书馆:
fmt::FormatInt(42).str(); // convert to std::string
fmt::FormatInt(42).c_str(); // convert and get as a C string
// (mind the lifetime, same as std::string::c_str())
根据 整串的基准转换 从提高业,这个方法的若干倍的速度比glibc的 sprintf
或 std::stringstream
.这是速度甚至比提高自己的业障 int_generator
为的是确认通过一个 独立基准.
免责声明:我是作者的这个图书馆。
我写了这个 线安全 功能一段时间前,我非常满意的结果和感觉到的算法是轻质的和精干、有效,是大约3倍的标准MSVC_itoa()function.
这里就是链接。 最佳的基-10仅投资()function? 性能至少10倍,sprintf().基准也是功能的质量保证测试,具体如下。
start = clock();
for (int i = LONG_MIN; i < LONG_MAX; i++) {
if (i != atoi(_i32toa(buff, (int32_t)i))) {
printf("\nError for %i", i);
}
if (!i) printf("\nAt zero");
}
printf("\nElapsed time was %f milliseconds", (double)clock() - (double)(start));
有一些愚蠢的建议作出的关于使用呼叫者的储存,会离开的结果浮动的地方在缓冲区的呼叫者的地址空间。忽略他们。代码我列出完美的作品,作为基准/QA码证明了这一点。
我相信这个代码瘦足以使用一个嵌入的环境。情况因人而异的,当然。
在Windows CE得出平台,有没有 iostream
s默认。要走的路有preferaby的 _itoa<>
家庭,通常 _itow<>
(由于大多数串的东西是Unicode的存在无论如何)。
上述大多数建议在技术上不C++,他们C的解决方案。
看起来进入使用的 std::stringstream.