c++operator用法
时间: 2023-10-28 16:07:16 浏览: 63
C++中的operator关键字用于重载操作符,使得用户可以自定义操作符的行为。通过重载操作符,可以使得用户自定义的类型像内置类型一样使用操作符。例如,可以重载"+"操作符来实现两个自定义类型对象的相加操作。同时,C++中也提供了一些内置的操作符可以被重载,例如"+","-","*","/"等等。
下面是一些C++ operator关键字的用法:
1. 重载一元操作符:可以重载"+","-","*","&","!"等一元操作符。
2. 重载二元操作符:可以重载"+","-","*","/","%","^","&","|","~","="等二元操作符。
3. 重载关系操作符:可以重载"==","!=","<",">","<=",">="等关系操作符。
4. 重载流操作符:可以重载">>","<<"等流操作符。
5. 重载函数调用操作符:可以重载"()"操作符,使得自定义类型的对象可以像函数一样被调用。
6. 重载下标操作符:可以重载"[]"操作符,使得自定义类型的对象可以像数组一样被访问。
相关问题
c++vector全部用法
C++中的vector是一个动态数组,可以在运行时动态地增加或减少其大小。以下是vector的全部用法:
1.创建vector:vector<int> vec; //创建一个空的vector
2.添加元素:vec.push_back(1); //在vector的末尾添加一个元素1
3.访问元素:vec; //访问vector中的第一个元素
4.获取vector的大小:vec.size(); //获取vector中元素的个数
5.使用迭代器访问元素:vector<int>::iterator it; for(it=vec.begin();it!=vec.end();it++) cout<<*it<<endl;
6.删除元素:vec.erase(vec.begin()+1); //删除vector中的第二个元素
7.清空vector:vec.clear(); //清空vector中的所有元素
8.判断vector是否为空:vec.empty(); //如果vector为空,则返回true,否则返回false
9.排序:sort(vec.begin(),vec.end()); //按升序排序
10.结构体作为vector的元素:typedef struct rect { int id; int length; int width; //对于向量元素是结构体的,可在结构体内部定义比较函数,下面按照id,length,width升序排序。 bool operator< (const rect &a) const { if(id!=a.id) return id<a.id; else { if(length!=a.length) return length<a.length; else return width<a.width; } } }Rect; vector<Rect> vec; Rect rect; rect.id=1; rect.length=2; rect.width=3; vec.push_back(rect); vector<Rect>::iterator it=vec.begin(); cout<<(*it).id<<' '<<(*it).length<<' '<<(*it).width<<endl;
11.自定义比较函数:bool Comp(const int &a,const int &b){ return a>b;}调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。
c++vector容器用法
c++中的vector是一个非常常用的容器,可以用来存储各种类型的数据。vector的基本操作包括头文件的引入,创建vector对象,插入元素,使用下标和迭代器访问元素等。具体操作如下:
(1) 头文件引入: #include<vector>
(2) 创建vector对象: vector<int> vec;
(3) 尾部插入数字: vec.push_back(a);
(4) 使用下标访问元素: cout<<vec[index];
(5) 使用迭代器访问元素:
vector<int>::iterator it;
for(it=vec.begin(); it!=vec.end(); it++)
cout<<*it<<endl;
除了存储基本数据类型,vector还可以存储结构体等自定义类型。需要注意的是,如果向量元素是结构体的话,需要在结构体内部定义比较函数以便进行排序等操作。
下面是一个简短的程序代码示例,展示了如何使用vector容器存储结构体类型的数据并进行访问:
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef struct rect {
int id;
int length;
int width;
bool operator< (const rect &a) const {
if(id != a.id)
return id < a.id;
else {
if(length != a.length)
return length < a.length;
else
return width < a.width;
}
}
} Rect;
int main() {
vector<Rect> vec;
Rect rect;
rect.id = 1;
rect.length = 2;
rect.width = 3;
vec.push_back(rect);
vector<Rect>::iterator it = vec.begin();
cout << (*it).id << ' ' << (*it).length << ' ' << (*it).width << endl;
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)