随笔分类 - c++语法
摘要:#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <random> #include "util.h" using namespace std; struct Point { in
阅读全文
摘要:基础的一个 cmake 文件: cmake_minimum_required(VERSION 3.25) set(CMAKE_CXX_STANDARD 20) project(app) # 可执行文件存储目录 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_
阅读全文
摘要:参考:指针声明 常量指针 (Pointer to Constant) 定义:一个指向常量的指针。这意味着指针指向的值不能被修改,但指针本身可以被修改,即可以重新指向其他地址。 声明:const Type* pointerName; const int* p1; int x = 10, y = 20;
阅读全文
摘要:参考:默认实参 #include <iostream> void f(int a, int b = 1); // 只要前面声明了 b 的值,后面 b 可以不给默认值 void f(int a = 2, int b) { std::cout << a << ' ' << b << '\n'; } in
阅读全文
摘要:#include <iostream> #include <string> class Token { public: Token() : tok(INT), ival(0) {} ~Token() { if (tok == STR) { sval.~basic_string(); // 联合体中编
阅读全文
摘要:## 二维数组分配: ```cpp #include int main() { int rows = 3; int cols = 4; // 使用二级指针创建一个动态分配的二维数组 int **array = new int*[rows]; for (int i = 0; i #include in
阅读全文
摘要:#include <iostream> #include <fstream> #include <sstream> int main() { std::ifstream file("in.txt"); if (!file.is_open()) { std::cerr << "can't open f
阅读全文
摘要:#include <iostream> class Student { public: Student(const std::string& name_, unsigned age_); ~Student() {} void output() const { std::cout << this->n
阅读全文
摘要:C++中的函数指针是一个指向函数的指针变量,它可以用来调用函数,也可以作为函数的参数传递给其他函数。函数指针的语法格式为: return_type (*pointer_name)(arg1_type, arg2_type, ...); 其中,return_type是函数的返回类型,pointer_n
阅读全文
摘要:普通左值引用:就是一个对象的别名,只能绑定左值,无法绑定常量对象 const int a = 10; int& ref_a = a; // wrong const左值引用:可以对常量起别名,可以绑定左值和右值 const int a = 10; const int& ref1 = a; // rig
阅读全文
摘要:auto只能推断出类型,引用不是类型,所以auto无法推断出引用,要使用引用只能自己加引用符号 auto关键字在推断引用的类型时:会直接将引用替换为引用指向的对象。其实引用一直是这样的,引用不是对象,任何使用引用的地方都可以直接替换成引用指向的对象 auto关键字在推断类型时,如果没有引用符号,会忽
阅读全文