Wonder奇迹奇迹

导航

C++中不同数据类型的互相转换

1.string --> char *
   char * p = (char*)str.c_str();

2.char * -->string
   char *p = "aaa";
   string str(p);

3.int-->string

int n=100000;
char t[38];
int i = 0;
if (n != 0)
{
while (n) {
t[i++] = (n % 10) + '0';
n /= 10;
}
t[i] = 0;
}
else
t[i] = '0';
return string(_strrev(t));

4.string-->number

#include <iostream>
#include <sstream>
template <class Type>
Type string2Num(const string& str)
{
      istringstream iss(str);
      Type num;
      iss >> num;
      return num;
}

待续....

posted on 2015-05-30 22:04  Wonder奇迹奇迹  阅读(403)  评论(0编辑  收藏  举报