小波变换与图注意力网络:交通流量预测的MATLAB实现
2026/9/19 17:56:54
C++ 标准只规定最小范围,具体大小由平台决定。常见 32/64 位平台典型大小:
| 类型 | 典型字节数 | 说明 |
|---|---|---|
| bool | 1 | true / false |
| char | 1 | 字符 / 小整数 |
| short | 2 | 短整型 |
| int | 4 | 整型(常用) |
| long | 4 / 8 | 长整型 |
| long long | 8 | 长长整型 |
| float | 4 | 单精度浮点 |
| double | 8 | 双精度浮点(常用) |
| long double | 8 / 16 | 扩展精度 |
unsigned char、unsigned int等,只存非负数。10u/10U→ unsigned10l/10L→ long10ll/10LL→ long long3.14f→ floatconstinta=10;a=20;// 错误constexprintsquare(intx){returnx*x;}intarr[square(5)];// 合法,编译期确定大小C++11 推荐统一使用列表初始化,更安全。
inta=10;// 拷贝初始化intb(10);// 直接初始化intc{10};// 列表初始化(推荐)intd={10};// 同上intx{3.14};// 编译报错,防止精度丢失=左边,有内存地址,可以被多次使用。inta=10;a=20;// a 是左值10;// 右值a+b;// 表达式结果是右值++i:先自增,返回自身(左值),效率更高。i++:先复制旧值,再自增,返回旧值(右值)。inti=0;cout<<++i;// 输出 1cout<<i++;// 输出 1,i 变为 2vector<int>v={1,2,3};for(intx:v){...}// 拷贝for(int&x:v){...}// 引用,可修改for(constint&x:v){...}// 只读,高效break。voidf(inta,intb=1,intc=2);// 正确voidf(inta=1,intb,intc);// 错误同一作用域内,函数名相同,参数列表不同(个数/类型/顺序)。
intadd(inta,intb);doubleadd(doublea,doubleb);// 重载inta=10;int*p=&a;// p 存 a 的地址*p=20;// 解引用,修改 anullptr(C++11),类型安全,优于NULL。delete后未置nullptr。p+1以指向类型大小为单位,只在同一数组内合法。inta=10;int&r=a;r=20;// a 也变成 20constint&r=10;// 合法栈 stack
堆 heap
new/malloc分配,手动管理。delete→内存泄漏。全局/静态区
static变量。常量区
const全局数据。代码区
new/delete是运算符,会调用构造/析构函数。malloc/free是库函数,只分配/释放裸内存。new↔deletenew[]↔delete[]free释放new出来的内存。delete释放malloc出来的内存。class默认private,struct默认public。private:仅本类/友元可访问。protected:本类/友元/子类可访问。public:全局可访问。编译器会自动生成,但若你手写其中一部分,另一些可能被删除。
Person(string n,inta):name(std::move(n)),age(a){}const成员~类名(),无参无返回,不可重载。Person(constPerson&other);Person&operator=(constPerson&other);if (this == &other) return *this;*this支持链式赋值T&&:专门绑定临时对象。Person(Person&&other)noexcept;Person&operator=(Person&&other)noexcept;std::move:将左值强制转为右值引用,本身不移动任何数据。类名* const。voidsetAge(intage){this->age=age;}*thisPerson&setName(string n){name=std::move(n);return*this;}p.setName("Alice").setAge(20);class子类:继承方式 父类{...};public继承:最常用,基类权限基本保留。protected继承:基类public变protected。private继承:基类所有成员变private。usingBase::func;多态 = 接口统一,实现不同。
满足三条件:
override:显式标记重写,编译器检查拼写/签名错误,强烈推荐。voidshow()override;final:virtualvoiddraw()=0;virtual,则通过基指针删除子类对象时,会多态调用子类析构。classBase{public:virtual~Base()=default;};classB:virtualpublicA{...};classC:virtualpublicA{...};template<typenameT>Tadd(T a,T b){returna+b;}template<typenameT,intN>Tsum(T(&arr)[N]){...}template<classT>classStack{vector<T>data;public:voidpush(constT&x);};Stack<int> st;template<>classStack<int>{...};template<classT>classStack<T*>{...};template<class...Args>voidprint(Args...args){...}...表示参数包。template<class...Args>autosum(Args...args){return(args+...);// 一元右折叠}constauto&x=y;vector<int>v;autoit=v.begin();inta=10;decltype(a)b=20;// intdecltype((a))r=a;// int&[capture](params)mutable->ret{body}[]不捕获[=]值捕获外部变量[&]引用捕获[this]捕获当前对象指针mutable:允许修改值捕获的副本。sort(v.begin(),v.end(),[](inta,intb){returna<b;});用于自动管理堆内存,避免泄漏/重复释放。
unique_ptr<int>p(newint(10));autop2=std::move(p);make_unique<T>(...)// C++14make_shared<T>(...)NULL。pair<int,string>f();auto[id,name]=f();vector、list、deque、array、stringset、map、multiset、multimap(红黑树,有序)unordered_set、unordered_map(哈希表,平均 O(1))stack、queue、priority_queuevectorlistdequemapunordered_map[begin(), end())左闭右开。cbegin()/cend():常量迭代器,不能修改元素。for_eachfind、find_if、count、binary_searchsort、stable_sortcopy、replace、fill、reversev.erase(remove(v.begin(),v.end(),val),v.end());try、throw、catchcatch (...)std::exception,提供what()信息。bad_alloc:new分配失败out_of_range:越界invalid_argument:无效参数lock_guard/unique_lockprivate/protected成员。<</>>、工具类访问内部数据。classA{friendvoidfunc(A&a);friendclassB;};返回值 operator op(参数)+、==)。::、.*、.、?:、sizeof、typeidT& operator++()T operator++(int)<</>>必须重载为非成员函数。#pragma once或#ifndef ... #define ... #endif。