C++_基础

数据类型

  • 基本数据类型

    • int

    • float和double

    • char

    • wchar_t

    • bool

    • void

  • 类型修饰符

    • signed

    • unsigned

    • short

    • long

  • 派生数据类型

    • 指针

    • 引用

    • 数组

    • 结构体

    • 枚举

    • 共用体

变量和常量

  • 变量

    • 整数类型(Integer Types):

      • int:用于表示整数,通常占用4个字节。
      • short:用于表示短整数,通常占用2个字节。
      • long:用于表示长整数,通常占用4个字节。
      • long long:用于表示更长的整数,通常占用8个字节。
    • 浮点类型(Floating-Point Types):

      • float:用于表示单精度浮点数,通常占用4个字节。
      • double:用于表示双精度浮点数,通常占用8个字节。
      • long double:用于表示更高精度的浮点数,占用字节数可以根据实现而变化。
    • 字符类型(Character Types):

      • char:用于表示字符,通常占用1个字节。
      • wchar_t:用于表示宽字符,通常占用2或4个字节。
      • char16_t:用于表示16位Unicode字符,占用2个字节。
      • char32_t:用于表示32位Unicode字符,占用4个字节。
    • 布尔类型(Boolean Type):

      • bool:用于表示布尔值,只能取truefalse
    • 枚举类型(Enumeration Types):

      • enum:用于定义一组命名的整数常量。
    • 指针类型(Pointer Types):

      • type*:用于表示指向类型为type的对象的指针。
    • 数组类型(Array Types):

      • type[]type[size]:用于表示具有相同类型的元素组成的数组。
    • 结构体类型(Structure Types):

      • struct:用于定义包含多个不同类型成员的结构。
    • 类类型(Class Types):

      • class:用于定义具有属性和方法的自定义类型。
    • 共用体类型(Union Types):

      • union:用于定义一种特殊的数据类型,它可以在相同的内存位置存储不同的数据类型。
  • 常量

    • 整数常量

    • 浮点常量

    • 布尔常量

    • 字符常量

    • 字符串常量

    • 定义常量

      • 使用 #define 预处理器。
      • 使用 const 关键字。

存储类

存储类定义 C++ 程序中变量/函数的范围(可见性)和生命周期。

  • auto:这是默认的存储类说明符,通常可以省略不写。auto 指定的变量具有自动存储期,即它们的生命周期仅限于定义它们的块(block)。auto 变量通常在栈上分配。

  • static:用于定义具有静态存储期的变量或函数,它们的生命周期贯穿整个程序的运行期。在函数内部,static变量的值在函数调用之间保持不变。在文件内部或全局作用域,static变量具有内部链接,只能在定义它们的文件中访问。

  • extern:用于声明具有外部链接的变量或函数,它们可以在多个文件之间共享。默认情况下,全局变量和函数具有 extern 存储类。在一个文件中使用extern声明另一个文件中定义的全局变量或函数,可以实现跨文件共享

STL

STL 分为多个组件,包括容器(Containers)、迭代器(Iterators)、算法(Algorithms)、函数对象(Function Objects)和适配器(Adapters)等

  • Containers

    • 序列容器:存储元素的序列,允许双向遍历。

      • std::vector:动态数组,支持快速随机访问。
      • std::deque:双端队列,支持快速插入和删除。
      • std::list:链表,支持快速插入和删除,但不支持随机访问。
    • 关联容器:存储键值对,每个元素都有一个键(key)和一个值(value),并且通过键来组织元素。
      • std::set:集合,不允许重复元素。
      • std::multiset:多重集合,允许多个元素具有相同的键。
      • std::map:映射,每个键映射到一个值。
      • std::multimap:多重映射,存储了键值对(pair),其中键是唯一的,但值可以重复,允许一个键映射到多个值。
    • 无序容器(C++11 引入):哈希表,支持快速的查找、插入和删除。
      • std::unordered_set:无序集合。
      • std::unordered_multiset:无序多重集合。
      • std::unordered_map:无序映射。
      • std::unordered_multimap:无序多重映射。
    • Iterators

    • Algorithms

日期和时间

C++ 标准库没有提供所谓的日期类型。C++ 继承了 C 语言用于日期和时间操作的结构和函数。为了使用日期和时间相关的函数和结构,需要在 C++ 程序中引用 <ctime> 头文件。+

  • 当前日期和时间

    • #include <iostream>
      #include <ctime>
       
      using namespace std;
       
      int main( )
      {
         // 基于当前系统的当前日期/时间
         time_t now = time(0);
         
         // 把 now 转换为字符串形式
         char* dt = ctime(&now);
       
         cout << "本地日期和时间:" << dt << endl;
       
         // 把 now 转换为 tm 结构
         tm *gmtm = gmtime(&now);
         dt = asctime(gmtm);
         cout << "UTC 日期和时间:"<< dt << endl;
      }
      本地日期和时间:Sat Jan  8 20:07:41 2011
      UTC 日期和时间:Sun Jan  9 03:07:41 2011
  • 使用结构 tm 格式化时间

    • #include <iostream>
      #include <ctime>
       
      using namespace std;
       
      int main( )
      {
         // 基于当前系统的当前日期/时间
         time_t now = time(0);
       
         cout << "1970 到目前经过秒数:" << now << endl;
       
         tm *ltm = localtime(&now);
       
         // 输出 tm 结构的各个组成部分
         cout << "年: "<< 1900 + ltm->tm_year << endl;
         cout << "月: "<< 1 + ltm->tm_mon<< endl;
         cout << "日: "<<  ltm->tm_mday << endl;
         cout << "时间: "<< ltm->tm_hour << ":";
         cout << ltm->tm_min << ":";
         cout << ltm->tm_sec << endl;
      }
      1970 到目前时间:1503564157
      年: 2017
      月: 8
      日: 24
      时间: 16:42:37
  • chrono库

<chrono>库的核心概念有以下几个:

    • Duration:表示一个时间间隔(持续时间),单位可以是秒、毫秒、微秒等。
      • std::chrono::duration用于表示时间段,类型为模板类,通常用秒、毫秒、微秒等单位来实例化:
        • #include <chrono>
          
          std::chrono::seconds sec(10);             // 10秒
          std::chrono::milliseconds ms(250);        // 250毫秒
          std::chrono::microseconds us(500);        // 500微秒
          std::chrono::nanoseconds ns(1000);        // 1000纳秒
          
          支持基本的数学操作,如加减:
          
          auto total = std::chrono::seconds(5) + std::chrono::milliseconds(500);  // 5.5秒
    • Time Point:表示一个时间点,通常基于某个时钟(clock)。
      • std::chrono::time_point表示某个时钟上的时间点,常和时钟类型一起使用:
        
        auto now = std::chrono::system_clock::now(); // 获取当前系统时间
        
        可以将时间点转换为时间戳(如转换为秒、毫秒等):
        
        auto duration_since_epoch = std::chrono::system_clock::now().time_since_epoch();
        auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(duration_since_epoch).count();
        std::cout << "Milliseconds since epoch: " << millis << std::endl;
    • Clock:提供当前时间,通常有三种时钟类型:系统时钟(system_clock)、高精度时钟(high_resolution_clock)和稳定时钟(steady_clock)。
      • std::chrono::system_clock:系统时钟,用于获取当前系统时间。
        std::chrono::steady_clock:稳定时钟,不会因系统时间调整而影响,适用于计时。
        std::chrono::high_resolution_clock:高精度时钟,用于精确计时。
        例如,使用steady_clock测量代码的运行时间:
        
        auto start = std::chrono::steady_clock::now();
        
        // 代码段
        std::this_thread::sleep_for(std::chrono::seconds(1));
        
        auto end = std::chrono::steady_clock::now();
        auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        std::cout << "Elapsed time: " << elapsed.count() << " ms" << std::endl;
    • 应用场景

      • 延迟操作

        • std::this_thread::sleep_for可以用来实现延迟操作,暂停一段时间:
          
          std::this_thread::sleep_for(std::chrono::seconds(2));  // 暂停2秒
          可以使用std::this_thread::sleep_until暂停到某个特定的时间点:
          
          auto future_time = std::chrono::system_clock::now() + std::chrono::seconds(5);
          std::this_thread::sleep_until(future_time);  // 暂停直到未来5秒的时间点
      • 实时监控

        • void monitorProcess() {
              while (true) {
                  auto start = std::chrono::steady_clock::now();
                  // 监控操作
                  auto end = std::chrono::steady_clock::now();
                  std::cout << "Monitoring duration: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
              }
          }

 文件操作

要在 C++ 中进行文件处理,必须在 C++ 源代码文件中包含头文件 <iostream> 和 <fstream>。

  • 打开文件

    • void open(const char *filename, ios::openmode mode);
  • 关闭文件

    • void close();
  • 读取文件

  在 C++ 编程中,我们使用流提取运算符( >> )从文件读取信息,就像使用该运算符从键盘输入信息一样。唯一不同的是,在这里您使用的是 ifstream 或 fstream 对象,而不是 cin 对象。

    • #include <fstream>
      #include <iostream>
      using namespace std;
       
      int main() {
          
         char data[100];
       
         // 以写模式打开文件
         ofstream outfile;
         outfile.open("afile.dat");
       
         cout << "Writing to the file" << endl;
         cout << "Enter your name: "; 
         cin.getline(data, 100);
       
         // 向文件写入用户输入的数据
         outfile << data << endl;
       
         cout << "Enter your age: "; 
         cin >> data;
         cin.ignore();
         
         // 再次向文件写入用户输入的数据
         outfile << data << endl;
       
         // 关闭打开的文件
         outfile.close();
       
         // 以读模式打开文件
         ifstream infile; 
         infile.open("afile.dat"); 
       
         cout << "Reading from the file" << endl; 
         infile >> data; 
       
         // 在屏幕上写入数据
         cout << data << endl;
         
         // 再次从文件读取数据,并显示它
         infile >> data; 
         cout << data << endl; 
       
         // 关闭打开的文件
         infile.close();
       
         return 0;
      }
      
      结果:
      Writing to the file
      Enter your name: Zara
      Enter your age: 9
      Reading from the file
      Zara
      9

       

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2025-10-07 13:37  直至成伤  阅读(4)  评论(0)    收藏  举报