c++中 int 和 string 的相互转换
2014-08-09 21:13
411 查看
/************************************************** author: Andrewseu date: 2014.8.8 **************************************************/ #include<iostream> #include<string> #include<strstream> using namespace std; int main() { /************************************************* string 转int: 方法一:int atoi( const char *str ); 功能:功能:将字符串str转换成一个整数并返回结果。 参数str 以数字开头,当函数从str 中读到非数字字符则 结束转换并将结果返回 方法二:std:to_string(); *************************************************/ //方法一 cout << "stringtoInt:" << atoi("1223243") << endl; string s = "45454"; cout << atoi(s.c_str()) << endl; //方法二 cout << std::to_string(43) << endl; /***************************************************** int 转 string: 方法一:itoa已经deprecation了,用_itoa; 方法一: int sprintf( char *buffer, const char *format, ... ); sprintf()函数和printf()类似, 只是把输出发送到buffer(缓冲区) 中.返回值是写入的字符数量 方法三:strstream *****************************************************/ //方法一: int test = 1234; char * cs = "sd"; char carray[20]; cout << _itoa(test,carray,10) << endl; //方法二: cout << sprintf(carray,"%d",test) << endl; string charString = carray; cout << charString << endl; //方法三: strstream strstr; string ss; strstr << test; strstr >> ss; cout << ss << endl; return 0; }
相关文章推荐
- C++里的int 和string类型相互转换
- C++中int与string的相互转换
- (c++) int 转 string,char*,const char*和string的相互转换
- c++中int,string,char类型的相互转换
- C++中int、string、const char*、CString之间的相互转换
- C++ 中 int,char*,string,CString之间相互转换-整理
- c++ int,double等数字类型与string的相互转换
- C++ int和string相互转换
- C++中string与int的相互转换实现代码
- C++中string,char,int,double等数据类型的相互转换及与ASCII码的转换
- C++中 char*, string, CString, int, double之间相互的转换
- c++中 int类型 与 string类型 的相互转换
- C++中int和string类型的相互转换
- c++中string与int之间相互转换
- c++ CString string char* char int类型的相互转换
- [C++] string与int, float, double相互转换
- C\C++:string与int相互转换1
- C++ 中使用sstream进行string和int相互转换
- (个人总结)C++中int类型与String类型的相互转换
- C++中int与string的相互转换