spo*_*son 173
在C++ 11中,您可以使用std::to_string:
#include <string>
std::string s = std::to_string(5);
Run Code Online (Sandbox Code Playgroud)
如果您在使用C++ 11之前,可以使用C++流:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
Run Code Online (Sandbox Code Playgroud)
取自http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
Leo*_*ans 54
boost :: lexical_cast工作得很好.
#include <boost/lexical_cast.hpp>
int main(int argc, char** argv) {
std::string foo = boost::lexical_cast<std::string>(argc);
}
Run Code Online (Sandbox Code Playgroud)
pae*_*bal 47
itoa是一个非标准的辅助函数,旨在补充atoi标准函数,并且可能隐藏了sprintf(其大部分功能可以用sprintf实现):http://www.cplusplus.com/reference/clibrary/cstdlib/ itoa.html
使用sprintf.或snprintf.或者你找到的任何工具.
尽管有些函数不在标准中,正如他的一篇评论中的"onebyone"正确提到的,大多数编译器都会为您提供一个替代方案(例如,Visual C++有自己的_snprintf,如果需要,可以输入sndef到snprintf).
使用C++流(在当前案例中为std :: stringstream(甚至是已弃用的std :: strstream,正如Herb Sutter在他的一本书中所提出的那样,因为它有点快).
你是C++,这意味着你可以选择你想要的方式:
更快的方式(即C方式),但你应该确保代码是你的应用程序的瓶颈(过早的优化是邪恶的,等等),并且你的代码被安全封装,以避免存在缓冲区溢出的风险.
更安全的方式(即C++的方式),如果你知道这部分代码并不重要,所以最好是确保这部分代码不会随意瞬间打破,因为有人误以为大小或指针(恰好在现实生活中,比如...昨天,在我的电脑上,因为有人认为使用更快的方式"很酷"而不需要它.
Jer*_*ten 34
试试sprintf():
char str[12];
int num = 3;
sprintf(str, "%d", num); // str now contains "3"
Run Code Online (Sandbox Code Playgroud)
sprintf()与printf()类似,但输出为字符串.
另外,正如Parappa在评论中提到的那样,您可能希望使用snprintf()来阻止缓冲区溢出(您转换的数字不符合字符串的大小.)它的工作方式如下:
snprintf(str, sizeof(str), "%d", num);
Run Code Online (Sandbox Code Playgroud)
180*_*ION 20
在幕后,lexical_cast执行此操作:
std::stringstream str;
str << myint;
std::string result;
str >> result;
Run Code Online (Sandbox Code Playgroud)
如果您不想为此"拖入"提升,那么使用上述方法是一个很好的解决方案.
Tag*_*318 10
我们可以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;
}
Run Code Online (Sandbox Code Playgroud)
别忘了#include <string>.
我使用这些模板
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;
}
Run Code Online (Sandbox Code Playgroud)
尝试使用Boost.Format或FastFormat这两个高质量的C++库:
int i = 10;
std::string result;
Run Code Online (Sandbox Code Playgroud)
用Boost.Format
result = str(boost::format("%1%", i));
Run Code Online (Sandbox Code Playgroud)
或FastFormat
fastformat::fmt(result, "{0}", i);
fastformat::write(result, i);
Run Code Online (Sandbox Code Playgroud)
显然,它们都比单个整数的简单转换做得更多
小智 6
您可以使用一个巧妙编写的模板函数将任何内容转换为字符串.此代码示例使用循环在Win-32系统中创建子目录.字符串连接运算符operator +用于将根与后缀连接以生成目录名.通过使用模板函数将循环控制变量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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
282556 次 |
| 最近记录: |