马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
数据范例
底子数据范例
整型(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_ptr、std::shared_ptr和std::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;
- }
复制代码
|