一、转 数字 1: //------------------------------------- 2: //功能:C++ string 转 int
3: //环境:VS2005
4: //-------------------------------------
5: 6: #include "stdafx.h"
7: #include <iostream> 8: #include <sstream> 9: 10: using namespace std; 11: 12: template <class Type> 13: Type stringToNum(const string& str) 14: { 15: istringstream iss(str); 16: Type num; 17: iss >> num; 18: return num;
19: } 20: 21: int main(int argc, char* argv[]) 22: { 23: string str("00801"); 24: cout << stringToNum<int>(str) << endl;
25: 26: system("pause");
27: return 0;
28: } 二、转 string itoa( //-------------------------------------
//功能:C++ int 转 string (使用atoi)
//环境:VS2005
//-------------------------------------
#include "stdafx.h"
#include <iostream> using namespace std; int main(int argc, char* argv[]) { int n = 30;
char c[10];
itoa(n, c, 2); cout << "2-> " << c << endl;
itoa(n, c, 10); cout << "16-> " << c << endl;
itoa(n, c, 16); cout << "10-> " << c << endl;
system("pause");
return 0;
} 参考http://pppboy.blog.163.com/blog/static/302037962010378296766/ |
|
来自: herowuking > 《C 》