C++中各类型之间的转换

在利用C++进行编程时,往往需要进行类型转换,C++中的类型转化不像python那样简洁,有的需要使用特定头文件下的方法

下面主要介绍C++中字符串,整型之间的转化

字符串  <---> 整型

字符串转整型

#include <stdio.h>      
#include <stdlib.h>     

int main ()
{
  char str[] = "256";
  int i_result;
  // 字符串转整型
  i_result = atoi(str);
  printf("%d\n", i_result);
  return 0;
}
执行结果:256

整型转字符串

#include <string> // string, std::to_string
using namespace std;
 
int main(){
    int n=100;
    string str=to_string(n);
    cout<<str<<endl;
    return 0;
}
执行结果:100 

posted on 2020-06-26 00:23  天池怪侠  阅读(190)  评论(0编辑  收藏  举报

导航