重载运算符
允许为用户定义的类型赋予运算符的功能
-
格式:
C++ 返回值类型 operator 符号 (const 参数类型 &参数名) const { // 运算符的实现逻辑 } -
举例:
-
对 轴上的线段封装结构体,按照线段左端点升序排序,左端点相同则按照右端点降序排序
可以直接调用
sort函数进行排序C++ struct node{ int l,r; bool operator < (const node &w) const{ if(l==w.l) return r>w.r; return l<w.l; } }; -
高精度模拟
C++ struct bint{//高精度非负整数 int a[100010],len;//长度不超过100000 bint(int x=0){//初始化值为x,默认x=0 memset(a,0,sizeof(a)); len=0; do{ a[len++]=x%10; x/=10; }while(x); } void carry(){//进位 for(int i=0;i<len;i++){ a[i+1]+=a[i]/10; a[i]%=10; } if(a[len]) len++; while(len>1&&!a[len-1]) len--; } bool operator < (const bint &w) const{ if(len==w.len){ for(int i=len-1;i>=0;i--){ if(a[i]!=w.a[i]){ return a[i]<w.a[i]; } } return 0; } return len<w.len; } bint operator + (const bint &w) const{ bint ans; ans.len=max(len,w.len); for(int i=0;i<ans.len;i++){ ans.a[i]=a[i]+w.a[i]; } ans.carry(); return ans; } bint operator * (const bint &w) const{ bint ans; ans.len=len+w.len-1; for(int i=0;i<len;i++){ for(int j=0;j<w.len;j++){ ans.a[i+j]+=a[i]*w.a[j]; } } ans.carry(); return ans; } bint operator - (const bint &w) const{//减去一个不大于自己的数 bint ans; ans.len=len; for(int i=0;i<len;i++){ ans.a[i]+=a[i]-w.a[i]; if(ans.a[i]<0){ ans.a[i]+=10; ans.a[i+1]--; } } ans.carry(); return ans; } void input(){ string s; cin>>s; len=s.size(); for(int i=len-1;i>=0;i--){ a[len-1-i]=s[i]-'0'; } } void output(){ for(int i=len-1;i>=0;i--) printf("%d",a[i]); puts(""); } };
-
pair
可以储存一对值的数据类型
-
创建方法:
C++ pair<T1,T2> name; -
赋值:
-
创建时赋值
C++ pair<int,double> p(3,0.618); -
访问内部赋值
C++ pair<string,bool> p; p.first="apple"; p.second=0; -
调用
make_pair赋值C++ pair<char,long long> p; p=make_pair('a',666);
-
-
比较规则:首先比较
first的值,如果相等再比较second的值
map
用于存储键值对的关联容器
-
特点:
- 每个元素是一个
pair,键唯一,但是值可以重复 - 会根据键的大小进行升序排序
- 基于红黑树实现,查找、插入、删除复杂度都是 ,空间复杂度为
- 每个元素是一个
-
创建方法:
C++ map<T1,T2> name; -
修改:
C++ map<char,int> mp; mp['a']=4;//将'a'键对应的值更新为4 -
访问:
C++ map<string,double> mp; cout<<mp["apple"];//输出键"apple"对应的值,如果不存在输出默认值0 -
遍历:
C++ map<int,char> mp; for(pair<int,char> i:mp){ printf("%d %c\n",i.first,i.second); } -
自定义结构体作为键:
C++ struct node{ int x,y; bool operator < (const node &w) const{ if(x==w.x) return y>w.y; return x<w.x; } }; map<node,int> mp;//键一定要重载小于号
unordered_map
用于存储键值对的关联容器
-
特点:
- 每个元素是一个
pair,键唯一,但是值可以重复 - 元素的存储顺序与插入顺序无关,取决于哈希函数的结果
- 查找、插入和删除的平均时间复杂度为 ,最坏情况下为 (当发生哈希冲突时)。
- 每个元素是一个
-
创建方法:
C++ unordered_map<T1,T2> name; -
修改:
C++ unordered_map<char,int> mp; mp['a']=4;//将'a'键对应的值更新为4 -
访问:
C++ unordered_map<string,double> mp; cout<<mp["apple"];//输出键"apple"对应的值,如果不存在输出默认值0 -
遍历:
C++ unordered_map<int,char> mp; for(pair<int,char> i:mp){ printf("%d %c\n",i.first,i.second); } -
自定义结构体作为键:需要定义键的哈希函数,不建议这么做
迭代器
用于访问容器中的元素
-
特点:
- 不同容器(如
vector,list,map)都可以通过迭代器以相同的方式访问元素。 - 迭代器可以使用指针操作(如解引用
*和递增++)。
- 不同容器(如
-
常用的迭代器:
begin():返回指向容器第一个元素的迭代器end():返回指向容器末尾之后位置的迭代器(不指向实际元素)。rbegin()和rend():返回反向迭代器,用于反向遍历。
-
迭代器常用操作:
*it:获取迭代器指向的元素。++it或者it++:移动到下一个元素。--it或者it--:移动到前一个元素。==和!=:判断是否相同
-
迭代器的创建:
C++ 容器类型<T1,T2,...>::iterator name;//创建迭代器 容器类型<T1,T2,...>::reverse_iterator name;//创建反向迭代器 -
举例:
-
使用迭代器访问
vector内部的所有元素C++ vector<int> a; vector<int>::iterator it; for(it=a.begin();it!=a.end();it++){ printf("%d ",*it); } -
使用迭代器反向访问
map内部的所有键值对C++ map<string,int> mp; map<string,int>::reverse_iterator it; for(it=mp.rbegin();it!=mp.rend();it++){ cout<<it->first<<" "<<it->second<<endl; }
-