C++入门基础:数据类型与条件判断语句

数据类型

基础数据类型

整型(Integral Types)

int:基本的整型,大小依赖于编译器和平台,通常是32位或64位。

short:短整型,通常是16位。 

long:长整型,大小依赖于编译器和平台,但至少是32位,可能是64位。

long long:长长整型,通常是64位。

#include <iostream>  
  
int main() {  
    int intVar = 12345;                  // 基本的整型  
    short shortVar = 123;               // 短整型  
    long longVar = 1234567890L;         // 长整型,使用L后缀  
    long long longLongVar = 1234567890123456789LL; // 加长整型,使用LL后缀  
  
    std::cout << "整型变量: " << intVar << std::endl;  
    std::cout << "短整型变量: " << shortVar << std::endl;  
    std::cout << "长整型变量: " << longVar << std::endl;  
    std::cout << "长长整型变量: " << longLongVar << std::endl;

    return 0;
}

字符型(Character Types)

char:字符类型,用于存储一个字符,占用1个字节。

#include <iostream>  
  
int main() {    
    char charVar = 'A';              
    std::cout << "字符型变量: " << charVar << std::endl; 
    return 0;  
} 
浮点型(Floating-Point Types)

float:单精度浮点数。

double:双精度浮点数。

long double:扩展的双精度浮点数,精度高于double。

#include <iostream>  
  
int main() {  
    float floatVar = 1.23f;             // 单精度浮点数,使用f后缀  
    double doubleVar = 2.34567;         // 双精度浮点数  
    long double longDoubleVar = 3.4567890123456789L; // 扩展的双精度浮点数,使用L后缀  
  
    std::cout << "单精度浮点数变量: " << floatVar << std::endl;  
    std::cout << "双精度浮点数变量: " << doubleVar << std::endl;  
    std::cout << "扩展的双精度浮点数变量: " << longDoubleVar << std::endl;  
  
    return 0;  
}

布尔型(Boolean Types)

bool:布尔类型,可以存储true或false。

#include <iostream>

int main()
{
    bool b = true;
    std::cout << b << std::endl;    // true会输出1,false会输出0
    bool boolVar = true;               
    std::cout << "布尔型变量: " << std::boolalpha << boolVar << std::endl; 
    return 0;
}

 复合数据类型

数组(Arrays)

存储相同类型数据的固定大小序列。

#include<iostream>

int main() {  
    int numbers[5] = {1, 2, 3, 4, 5};  
    for (int i = 0; i < 5; ++i) {  
        std::cout << "Element at index " << i << ": " << numbers[i] << std::endl;  
    }  
    return 0;
}

字符串(Strings)

在C++中,字符串通常由 std::string 类表示。

#include<iostream>

int main() {  
    std::string greeting = "Hello, World!";  
    std::cout << "Greeting: " << greeting << std::endl;  
    return 0;
}
结构体(Structures)

用户定义的数据类型,可以包含多个不同类型的数据成员。

#include <iostream>  
#include <string>  

struct Person {  // 定义结构体  
    std::string name;  
    int age;  
};  

int main()
{
    Person p;  // 声明结构体变量  
    p.name = "GGBond";  // 赋值  
    p.age = 18;  
    std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl;  
    return 0;
}

联合体(Unions)

union 允许在相同的内存位置存储不同的数据类型,但一次只能使用其中一个成员。

#include <iostream>  
#include <string>  

// 定义联合体 
union Data {  
    int intValue;  
    float floatValue;  
    char charValue;  
};  

int main()
{
        Data data;  
    data.intValue = 100;  
    std::cout << "Union intValue: " << data.intValue << std::endl;  
    data.floatValue = 3.14f; // 覆盖上面的intValue  
    std::cout << "Union floatValue: " << data.floatValue << std::endl;  
    return 0;
}

 

枚举(Enumerations)

enum: 一种用户定义的类型,包含固定数量的常量。

#include <iostream>  
#include <string>  

enum Pig { GGBond, FeiFei, MiHu };  

int main() {  
    Pig pig = FeiFei;  
    std::cout << pig << std::endl;  
    return 0;  
}

指针和引用类型

指针(Pointers)

存储内存地址的变量。

引用(References)

对象的别名,提供了对对象的直接访问。

#include <iostream>  
  
int main() {  
    // 使用指针  
    int value = 42;  
    int *pointerToValue = &value; // 指针存储value的内存地址  
    std::cout << "Value: " << value << std::endl;  
    std::cout << "Address of value: " << &value << std::endl;  
    std::cout << "Value pointed by pointerToValue: " << *pointerToValue << std::endl;  
    std::cout << "Address stored in pointerToValue: " << pointerToValue << std::endl;  
  
    // 引用指针  
    int &referenceToValue = value; // 引用是value的别名  
    referenceToValue = 100; // 修改引用的值,实际上修改了value的值  
    std::cout << "Value after modification through reference: " << value << std::endl;  
  
    return 0;  
}

用户自定义数据类型

类(Classes)

用户定义的类型,可以包含数据成员和成员函数,支持继承、多态等面向对象特性。

#include <iostream>  
#include <string>  
  
// 定义类Person,类中有name和age两个成员变量  
class Person {  
private:  
    std::string name;  
    int age;  
  
public:  
    // 构造函数  
    Person(const std::string &n, int a) : name(n), age(a) {}  
    // 成员函数  
    void introduce() {  
        std::cout << "My name is " << name << " and I am " << age << " years old." << std::endl;  
    }  
    // 访问器(getter)  
    int getAge() const {  
        return age;  
    }  
    // 修改器(setter)  
    void setAge(int newAge) {  
        age = newAge;  
    }  
};  
  
int main() {  
    // 创建Person类的实例  
    Person person("GGBond", 18);  
    person.introduce(); // 调用成员函数  
    // 使用访问器和修改器  
    std::cout << "Original age: " << person.getAge() << std::endl;  
    person.setAge(35);  
    std::cout << "Updated age: " << person.getAge() << std::endl;  
  
    return 0;  
}

C++11及之后引入的新数据类型

空指针类型(nullptr_t)

表示空指针的类型,用于替代C++03中的NULL宏。

#include <iostream>  
  
int main() {  
    // 使用nullptr初始化指针  
    int* ptr = nullptr;  
  
    if (ptr == nullptr) {  
        std::cout << "Pointer is null." << std::endl;  
    } else {  
        std::cout << "Pointer is not null." << std::endl;  
    }  
  
    return 0;  
}

 

自动类型推导的变量(auto)

编译器根据初始化表达式自动推断变量的类型。

#include <iostream>  
#include <vector>  
  
int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
  
    // 使用auto关键字自动推导迭代器类型  
    for (auto it = vec.begin(); it != vec.end(); ++it) {  
        std::cout << *it << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}

输出:1 2 3 4 5 

范围基础的for循环(Range-based for loop)

虽然不算是一个数据类型,但C++11引入了新的循环语法,它依赖于范围的概念。

#include <iostream>  
#include <vector>  
  
int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
  
    // 使用范围基础的for循环遍历容器  
    for (const auto& value : vec) {  
        std::cout << value << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}

 

智能指针(Smart Pointers)

std::unique_ptrstd::shared_ptrstd::weak_ptr,用于自动管理动态分配的内存。

#include <iostream>  
#include <memory>  
  
struct Example {  
    Example(int value) : value_(value) {}  
    ~Example() { std::cout << "Destroying Example with value " << value_ << std::endl; }  
    int value_;  
};  
  
int main() {  
    // 使用std::unique_ptr管理动态内存  
    std::unique_ptr<Example> examplePtr(new Example(42));  
  
    // 当unique_ptr超出范围时,它会自动删除所指向的对象  
    return 0;  
}

 

右值引用和移动语义(Rvalue References and Move Semantics)

C++11引入了右值引用,以支持移动语义和完美转发。

#include <iostream>  
#include <string>  
#include <utility> // 使用std::move  
  
void processString(std::string&& str) {  
    std::cout << "Processing moved string: " << str << std::endl;  
}  
  
int main() {  
    std::string str = "Hello, World!";  
    // 使用std::move将左值转换为右值引用,实现移动语义, 移动后,str的状态是未定义的,使用前需要重新赋值或清除。  
    processString(std::move(str));  
  
    return 0;  
}

 

初始化列表(Initializer Lists)

允许使用花括号初始化对象和容器。

#include <iostream>  
#include <vector>  
#include <initializer_list>  
  
int main() {  
    // 使用初始化列表初始化vector  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
  
    for (const auto& value : vec) {  
        std::cout << value << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}

 

条件判断语句

if语句

if语句是最基础的条件判断语句 ,当只需要在条件为真时执行操作,而不需要处理条件为假的情况。

例如:如果数值 number 大于5,在终端输出 数值比5大

#include <iostream>  
using namespace std;  
  
int main() {  
    int number = 10;  
  
    if (number > 5) {  
        cout << "数值比5大" << endl;  
    }  
  
    return 0;  
}

if-else语句

if-else语句在if语句的基础上增加了对条件为假时的处理,当需要在条件为真时执行一段代码,条件为假时执行另一段代码。

例如:如果数值 number 大于5,在终端输出数值比5大,如果数值 number 小于5,在终端输出数值比5小

#include <iostream>  
using namespace std;  
  
int main() {  
    int number;  

    std::cout << "请输入一个整数: ";
    std::cin >> number;
  
    if (number > 5) {  
        cout << "数值比5大" << endl;  
    } else {  
        cout << "数值比5小" << endl;  
    }  
  
    return 0;  
}

 if-else-if语句

 if-else if-else语句允许对多个条件进行判断,并分别处理。

#include <iostream>  
using namespace std;  
  
int main() {  
    
    std::cout << "输入成绩: ";
    int grade;
    std::cin >> grade;
  
    if (grade >= 90) {  
        cout << "你的成绩是 A." << endl;  
    } else if (grade >= 80) {  
        cout << "你的成绩是 B." << endl;  
    } else if (grade >= 70) {  
        cout << "你的成绩是 C." << endl;  
    } else if (grade >= 60) {  
        cout << "你的成绩是 D." << endl;  
    } else {  
        cout << "你的成绩是 E." << endl;  
    }  
  
    return 0;  
}

 switch语句

switch语句用于对某个表达式的值进行判断,并与多个case标签进行比较,当需要根据某个变量的不同值执行不同代码块时,特别是该变量为整型或枚举类型时。

#include <iostream>  
using namespace std;  
  
int main() {  
    char day ;
    cout << "请输入评分等级(A, B, C, D, E): ";  
    cin >> day; 
  
    switch (day) {  
        case 'A':  
            cout << "90分以上" << endl;  
            break;  
        case 'B':  
            cout << "80~90分" << endl;  
            break;  
        case 'C':  
            cout << "70~80分" << endl;  
            break;  
        case 'D':  
            cout << "60~70分" << endl; 
            break;  
        case 'E':  
            cout << "不合格" << endl;  
            break;  
        default:  
            cout << "没有这个等级" << endl;  
            break;  
    }  
  
    return 0;  
}

 

  • 15
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值