c++语言中类型的转换

// test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdio.h"
#include <string> 
#include <cstdlib>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    string str = "1234";              
    const char * a = str.c_str();     //将string 类型转换为const char * 类型
    int i = atoi(a);                  //字符串const char *转换成int 型
    i++;
    printf("the value of i is %d",i);
    getchar();
    return 0;
}
可以看到,c++对类型比较严格,以后会遇到各种奇葩的转化,以后有时间会画一个转化图。

参考资料:
【1】<<剑指offer 案例1>> p245
【2】c++参考官网http://www.cplusplus.com/reference/string/

posted @ 2017-03-06 15:12  开往春天的拖拉机  阅读(88)  评论(0编辑  收藏  举报