C++ 基本数据类型

C++的类型比较多,本文只列工作中常用的类型

1. 基本数据类型

int  //整型  4个字节
bool  //布尔型  1个字节
char  //字符型  1个字节  
float  //浮点型(单精度)  4个字节
double  //双浮点型(双精度)  8个字节
std::string   //其实是容器,类型转换使用

类型修饰符

signed  //定义的类型默认为有符号
unsigned   //无符号
short   //
long    //

以下为stdint.h文件中定义的类型,不过一般用int类型较多(int32_t和int64_t等),不同系统int的位数不一致,使用int32_t等可以增加系统之间的可移植性;

typedef signed char        int8_t;
typedef short              int16_t;
typedef int                int32_t;
typedef long long          int64_t;
typedef unsigned char      uint8_t;
typedef unsigned short     uint16_t;
typedef unsigned int       uint32_t;
typedef unsigned long long uint64_t;

typedef signed char        int_least8_t;
typedef short              int_least16_t;
typedef int                int_least32_t;
typedef long long          int_least64_t;
typedef unsigned char      uint_least8_t;
typedef unsigned short     uint_least16_t;
typedef unsigned int       uint_least32_t;
typedef unsigned long long uint_least64_t;

typedef signed char        int_fast8_t;
typedef int                int_fast16_t;
typedef int                int_fast32_t;
typedef long long          int_fast64_t;
typedef unsigned char      uint_fast8_t;
typedef unsigned int       uint_fast16_t;
typedef unsigned int       uint_fast32_t;
typedef unsigned long long uint_fast64_t;

typedef long long          intmax_t;
typedef unsigned long long uintmax_t;

2. 变量的命名

变量的命名多种多样,形成自己的风格即可(当然前提要符合公司的规范)

匈牙利命名法

变量前面加前缀,表示类型,例如iCount、iSum表示int类型

驼峰式命名法

首字母大写,例如StudentNumber

snake_case

变量全是小写字符,中间用 “_” 隔开

C++相关的一般使用snake_case,同时再加上其他的一些特性

#define  MAX_PERSON_NUMBER   //常量全部大写
namespace person_data   //命名空间全部小写
int32_t g_flag;     //全局变量加g
int32_t student_number;  //一般变量,小写字母+下划线  
class  PersonInfo{}   //类名用大写
int32_t m_number;    //类的成员变量, m表示member variable
string m_name;
void cal_number()  //成员函数全部小写

成员变量和成员函数也可以写成

int32_t number_;
string name_;
void calNumber()  //成员函数全部小写

3. 类型之间的转换

#include <iostream>
#include <string>
#include <cmath>

int main(int argc, char *argv[])
{
    int32_t person_number = 99;
    std::string student_number = "36";
    std::string student_name = "wang";
    const char* teacher_number = "54";
    float score = 98.87;

    //int -> string
    std::string result_person_number = std::to_string(person_number);
    std::cout<<"int -> string"<<std::endl;
    std::cout<<"result_person_number is: "<<result_person_number<<std::endl<<std::endl;

    //string -> int
    int32_t result_student_number = std::atoi(student_number.c_str());   //std::atof是转换为浮点型
    std::cout<<"string -> int"<<std::endl;
    std::cout<<"result_student_number is: "<<result_student_number<<std::endl<<std::endl;    

    //char* -> string
    std::cout<<"char* -> string"<<std::endl;
    std::string result_teacher_number = teacher_number;   //直接复制
    std::cout<<"result_teacher_number is: "<<result_teacher_number<<std::endl<<std::endl;    

    //string -> char* 
    std::cout<<"string -> char* "<<std::endl;
    const char* result_student_name = student_name.c_str();
    std::cout<<"result_student_name is: "<<result_student_name<<std::endl<<std::endl;    
    
    //double -> int
    std::cout<<"double -> int"<<std::endl;
    int32_t score_up = ceil(score);   //向上去整
    int32_t score_down = floor(score);   //向下取整
    std::cout<<"score_up is: "<<score_up<<std::endl;
    std::cout<<"score_down is: "<<score_down<<std::endl<<std::endl;
    

    //判断字符是否数字,大写字符,小写字符
    //std::isupper检测字符是否为大写
    std::string value = "A";
    //char value = 'A';   //如果使用字符注意修改后面的内容
    if(std::isupper(value[0]))
    {
        std::cout<<"value is Capital letter."<<std::endl;
    }

    //std::islower
    value = "a";
    if(std::islower(value[0]))
    {
        std::cout<<"value is Lowercase letter."<<std::endl;
    }

    //std::isdigit检测字符是否为数字
    value = "5";
    if(std::isdigit(value[0]))
    {
        std::cout<<"value is number."<<std::endl;
    }

    return 0;
}

输出:

int -> string
result_person_number is: 99

string -> int
result_student_number is: 36

char* -> string
result_teacher_number is: 54

string -> char*
result_student_name is: wang

double -> int
score_up is: 99
score_down is: 98

value is Capital letter.
value is Lowercase letter.
value is number.
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值