C++入门底子:数据范例与条件判定语句

[复制链接]
发表于 2026-2-10 03:22:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
数据范例

底子数据范例

整型(Integral Types)

int:根本的整型,巨细依赖于编译器宁静台,通常是32位或64位。
short:短整型,通常是16位。 
long:长整型,巨细依赖于编译器宁静台,但至少是32位,大概是64位。
long long:长长整型,通常是64位。
  1. #include <iostream>  
  2.   
  3. int main() {  
  4.     int intVar = 12345;                  // 基本的整型  
  5.     short shortVar = 123;               // 短整型  
  6.     long longVar = 1234567890L;         // 长整型,使用L后缀  
  7.     long long longLongVar = 1234567890123456789LL; // 加长整型,使用LL后缀  
  8.   
  9.     std::cout << "整型变量: " << intVar << std::endl;  
  10.     std::cout << "短整型变量: " << shortVar << std::endl;  
  11.     std::cout << "长整型变量: " << longVar << std::endl;  
  12.     std::cout << "长长整型变量: " << longLongVar << std::endl;
  13.     return 0;
  14. }
复制代码

字符型(Character Types)

char:字符范例,用于存储一个字符,占用1个字节。
  1. #include <iostream>  
  2.   
  3. int main() {   
  4.     char charVar = 'A';              
  5.     std::cout << "字符型变量: " << charVar << std::endl;
  6.     return 0;  
  7. }
复制代码
浮点型(Floating-Point Types)

float:单精度浮点数。
double:双精度浮点数。
long double:扩展的双精度浮点数,精度高于double。
  1. #include <iostream>  
  2.   
  3. int main() {  
  4.     float floatVar = 1.23f;             // 单精度浮点数,使用f后缀  
  5.     double doubleVar = 2.34567;         // 双精度浮点数  
  6.     long double longDoubleVar = 3.4567890123456789L; // 扩展的双精度浮点数,使用L后缀  
  7.   
  8.     std::cout << "单精度浮点数变量: " << floatVar << std::endl;  
  9.     std::cout << "双精度浮点数变量: " << doubleVar << std::endl;  
  10.     std::cout << "扩展的双精度浮点数变量: " << longDoubleVar << std::endl;  
  11.   
  12.     return 0;  
  13. }
复制代码

布尔型(Boolean Types)

bool:布尔范例,可以存储true或false。
  1. #include <iostream>
  2. int main()
  3. {
  4.     bool b = true;
  5.     std::cout << b << std::endl;    // true会输出1,false会输出0
  6.     bool boolVar = true;               
  7.     std::cout << "布尔型变量: " << std::boolalpha << boolVar << std::endl;
  8.     return 0;
  9. }
复制代码
 复合数据范例

数组(Arrays)

存储雷同范例数据的固定巨细序列。
  1. #include<iostream>
  2. int main() {  
  3.     int numbers[5] = {1, 2, 3, 4, 5};  
  4.     for (int i = 0; i < 5; ++i) {  
  5.         std::cout << "Element at index " << i << ": " << numbers[i] << std::endl;  
  6.     }  
  7.     return 0;
  8. }
复制代码

字符串(Strings)

在C++中,字符勾通常由 std::string 类表现。
  1. #include<iostream>
  2. int main() {  
  3.     std::string greeting = "Hello, World!";  
  4.     std::cout << "Greeting: " << greeting << std::endl;  
  5.     return 0;
  6. }
复制代码
布局体(Structures)

用户界说的数据范例,可以包罗多个差异范例的数据成员。
  1. #include <iostream>  
  2. #include <string>  
  3. struct Person {  // 定义结构体  
  4.     std::string name;  
  5.     int age;  
  6. };  
  7. int main()
  8. {
  9.     Person p;  // 声明结构体变量  
  10.     p.name = "GGBond";  // 赋值  
  11.     p.age = 18;  
  12.     std::cout << "Name: " << p.name << ", Age: " << p.age << std::endl;  
  13.     return 0;
  14. }
复制代码

团结体(Unions)

union 允许在雷同的内存位置存储差异的数据范例,但一次只能使用此中一个成员。
  1. #include <iostream>  
  2. #include <string>  
  3. // 定义联合体
  4. union Data {  
  5.     int intValue;  
  6.     float floatValue;  
  7.     char charValue;  
  8. };  
  9. int main()
  10. {
  11.         Data data;  
  12.     data.intValue = 100;  
  13.     std::cout << "Union intValue: " << data.intValue << std::endl;  
  14.     data.floatValue = 3.14f; // 覆盖上面的intValue  
  15.     std::cout << "Union floatValue: " << data.floatValue << std::endl;  
  16.     return 0;
  17. }
复制代码
 

摆列(Enumerations)

enum: 一种用户界说的范例,包罗固定命量的常量。
  1. #include <iostream>  
  2. #include <string>  
  3. enum Pig { GGBond, FeiFei, MiHu };  
  4. int main() {  
  5.     Pig pig = FeiFei;  
  6.     std::cout << pig << std::endl;  
  7.     return 0;  
  8. }
复制代码

指针和引用范例

指针(Pointers)

存储内存地点的变量。
引用(References)

对象的别名,提供了对对象的直接访问。
  1. #include <iostream>  
  2.   
  3. int main() {  
  4.     // 使用指针  
  5.     int value = 42;  
  6.     int *pointerToValue = &value; // 指针存储value的内存地址  
  7.     std::cout << "Value: " << value << std::endl;  
  8.     std::cout << "Address of value: " << &value << std::endl;  
  9.     std::cout << "Value pointed by pointerToValue: " << *pointerToValue << std::endl;  
  10.     std::cout << "Address stored in pointerToValue: " << pointerToValue << std::endl;  
  11.   
  12.     // 引用指针  
  13.     int &referenceToValue = value; // 引用是value的别名  
  14.     referenceToValue = 100; // 修改引用的值,实际上修改了value的值  
  15.     std::cout << "Value after modification through reference: " << value << std::endl;  
  16.   
  17.     return 0;  
  18. }
复制代码

用户自界说数据范例

类(Classes)

用户界说的范例,可以包罗数据成员和成员函数,支持继续、多态等面向对象特性。
  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. // 定义类Person,类中有name和age两个成员变量  
  5. class Person {  
  6. private:  
  7.     std::string name;  
  8.     int age;  
  9.   
  10. public:  
  11.     // 构造函数  
  12.     Person(const std::string &n, int a) : name(n), age(a) {}  
  13.     // 成员函数  
  14.     void introduce() {  
  15.         std::cout << "My name is " << name << " and I am " << age << " years old." << std::endl;  
  16.     }  
  17.     // 访问器(getter)  
  18.     int getAge() const {  
  19.         return age;  
  20.     }  
  21.     // 修改器(setter)  
  22.     void setAge(int newAge) {  
  23.         age = newAge;  
  24.     }  
  25. };  
  26.   
  27. int main() {  
  28.     // 创建Person类的实例  
  29.     Person person("GGBond", 18);  
  30.     person.introduce(); // 调用成员函数  
  31.     // 使用访问器和修改器  
  32.     std::cout << "Original age: " << person.getAge() << std::endl;  
  33.     person.setAge(35);  
  34.     std::cout << "Updated age: " << person.getAge() << std::endl;  
  35.   
  36.     return 0;  
  37. }
复制代码

C++11及之后引入的新数据范例

空指针范例(nullptr_t)

表现空指针的范例,用于更换C++03中的NULL宏。
  1. #include <iostream>  
  2.   
  3. int main() {  
  4.     // 使用nullptr初始化指针  
  5.     int* ptr = nullptr;  
  6.   
  7.     if (ptr == nullptr) {  
  8.         std::cout << "Pointer is null." << std::endl;  
  9.     } else {  
  10.         std::cout << "Pointer is not null." << std::endl;  
  11.     }  
  12.   
  13.     return 0;  
  14. }
复制代码
 

主动范例推导的变量(auto)

编译器根据初始化表达式主动推断变量的范例。
  1. #include <iostream>  
  2. #include <vector>  
  3.   
  4. int main() {  
  5.     std::vector<int> vec = {1, 2, 3, 4, 5};  
  6.   
  7.     // 使用auto关键字自动推导迭代器类型  
  8.     for (auto it = vec.begin(); it != vec.end(); ++it) {  
  9.         std::cout << *it << " ";  
  10.     }  
  11.     std::cout << std::endl;  
  12.   
  13.     return 0;  
  14. }
复制代码
输出:1 2 3 4 5 
范围底子的for循环(Range-based for loop)

固然不算是一个数据范例,但C++11引入了新的循环语法,它依赖于范围的概念。
  1. #include <iostream>  
  2. #include <vector>  
  3.   
  4. int main() {  
  5.     std::vector<int> vec = {1, 2, 3, 4, 5};  
  6.   
  7.     // 使用范围基础的for循环遍历容器  
  8.     for (const auto& value : vec) {  
  9.         std::cout << value << " ";  
  10.     }  
  11.     std::cout << std::endl;  
  12.   
  13.     return 0;  
  14. }
复制代码
 

智能指针(Smart Pointers)

如std::unique_ptr、std::shared_ptr和std::weak_ptr,用于主动管理动态分配的内存。
  1. #include <iostream>  
  2. #include <memory>  
  3.   
  4. struct Example {  
  5.     Example(int value) : value_(value) {}  
  6.     ~Example() { std::cout << "Destroying Example with value " << value_ << std::endl; }  
  7.     int value_;  
  8. };  
  9.   
  10. int main() {  
  11.     // 使用std::unique_ptr管理动态内存  
  12.     std::unique_ptr<Example> examplePtr(new Example(42));  
  13.   
  14.     // 当unique_ptr超出范围时,它会自动删除所指向的对象  
  15.     return 0;  
  16. }
复制代码
 

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

C++11引入了右值引用,以支持移动语义和完满转发。
  1. #include <iostream>  
  2. #include <string>  
  3. #include <utility> // 使用std::move  
  4.   
  5. void processString(std::string&& str) {  
  6.     std::cout << "Processing moved string: " << str << std::endl;  
  7. }  
  8.   
  9. int main() {  
  10.     std::string str = "Hello, World!";  
  11.     // 使用std::move将左值转换为右值引用,实现移动语义, 移动后,str的状态是未定义的,使用前需要重新赋值或清除。  
  12.     processString(std::move(str));  
  13.   
  14.     return 0;  
  15. }
复制代码
 

初始化列表(Initializer Lists)

允许使用花括号初始化对象和容器。
  1. #include <iostream>  
  2. #include <vector>  
  3. #include <initializer_list>  
  4.   
  5. int main() {  
  6.     // 使用初始化列表初始化vector  
  7.     std::vector<int> vec = {1, 2, 3, 4, 5};  
  8.   
  9.     for (const auto& value : vec) {  
  10.         std::cout << value << " ";  
  11.     }  
  12.     std::cout << std::endl;  
  13.   
  14.     return 0;  
  15. }
复制代码
 

条件判定语句

if语句

if语句是最底子的条件判定语句 ,当只必要在条件为真时实行使用,而不必要处理处罚条件为假的情况。
比方:假如数值 number 大于5,在终端输出 数值比5大
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     int number = 10;  
  6.   
  7.     if (number > 5) {  
  8.         cout << "数值比5大" << endl;  
  9.     }  
  10.   
  11.     return 0;  
  12. }
复制代码
if-else语句

if-else语句在if语句的底子上增长了对条件为假时的处理处罚,当必要在条件为真时实行一段代码,条件为假时实行另一段代码。
比方:假如数值 number 大于5,在终端输出数值比5大,假如数值 number 小于5,在终端输出数值比5小
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     int number;  
  6.     std::cout << "请输入一个整数: ";
  7.     std::cin >> number;
  8.   
  9.     if (number > 5) {  
  10.         cout << "数值比5大" << endl;  
  11.     } else {  
  12.         cout << "数值比5小" << endl;  
  13.     }  
  14.   
  15.     return 0;  
  16. }
复制代码

 if-else-if语句

 if-else if-else语句允许对多个条件举行判定,并分别处理处罚。
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.    
  6.     std::cout << "输入成绩: ";
  7.     int grade;
  8.     std::cin >> grade;
  9.   
  10.     if (grade >= 90) {  
  11.         cout << "你的成绩是 A." << endl;  
  12.     } else if (grade >= 80) {  
  13.         cout << "你的成绩是 B." << endl;  
  14.     } else if (grade >= 70) {  
  15.         cout << "你的成绩是 C." << endl;  
  16.     } else if (grade >= 60) {  
  17.         cout << "你的成绩是 D." << endl;  
  18.     } else {  
  19.         cout << "你的成绩是 E." << endl;  
  20.     }  
  21.   
  22.     return 0;  
  23. }
复制代码

 switch语句

switch语句用于对某个表达式的值举行判定,并与多个case标签举行比力,当必要根据某个变量的差异值实行差异代码块时,特殊是该变量为整型或摆列范例时。
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. int main() {  
  5.     char day ;
  6.     cout << "请输入评分等级(A, B, C, D, E): ";  
  7.     cin >> day;
  8.   
  9.     switch (day) {  
  10.         case 'A':  
  11.             cout << "90分以上" << endl;  
  12.             break;  
  13.         case 'B':  
  14.             cout << "80~90分" << endl;  
  15.             break;  
  16.         case 'C':  
  17.             cout << "70~80分" << endl;  
  18.             break;  
  19.         case 'D':  
  20.             cout << "60~70分" << endl;
  21.             break;  
  22.         case 'E':  
  23.             cout << "不合格" << endl;  
  24.             break;  
  25.         default:  
  26.             cout << "没有这个等级" << endl;  
  27.             break;  
  28.     }  
  29.   
  30.     return 0;  
  31. }
复制代码

 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表