- 重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:
不改变运算符的优先级不改变运算符的结合性不改变运算符所需要的操作数不能创建新的运算符
-
运算符函数可以重载为成员函数或友元函数
- 重载为成员函数,解释为:←←一元
Object . operator op ()
操作数由对象Object通过this指针隐含传递
重载为友元函数,解释为:
operator op (Object)
操作数由参数表的参数Object提供 - 重载为成员函数,解释为:←←二元
ObjectL . operator op ( ObjectR )
左操作数由ObjectL通过this指针传递,右操作数由参数ObjectR传递
重载为友元函数,解释为:
operator op ( ObjectL, ObjectR )
左右操作数都由参数传递 - STL容器的共同操作:
std::list<int> l;
以另一个容器元素为初值完成初始化:
std::list<int> l;
…
std::vector<float> c(l.begin(),l.end());
以数组元素为初值完成初始化:
int array[]={2,4,6,1345};
…
std::set<int> c(array,array+sizeof(array)/sizeof(array[0]));
与大小相关的操作(size operator):
size() //返回当前容器的元素数量
empty() //判断容器是否为空
max_size() //返回容器能容纳的最大元素数量
比较(comparison):
赋值(assignment)和交换(swap):==,!=,<,<=,>,>=比较操作两端的容器必须属于同一类型如果两个容器内的所有元素按序相等,那么这两个容器相等采用字典式顺序判断某个容器是否小于另一个容器
与迭代器(iterator)相关的操作:swap用于提高赋值操作效率
begin() //返回一个迭代器,指向第一个元素
end() //返回一个迭代器,指向最后一个元素之后
rbegin() //返回一个逆向迭代器,指向逆向遍历的第一个元素
rend() //返回一个逆向迭代器,指向逆向遍历的最后一个元素之后
元素操作
insert(pos,e) //将元素e的拷贝安插于迭代器pos所指的位置
erase(beg,end) //移除[beg,end]区间内的所有元素
clear() //移除所有元素
例子:
例1:重载<<用于输出
- class Time
- {
- private:
- int month;
- int day;
- int hour;
- int minute;
- public:
- Time():month(1),day(1),hour(0),minute(0){};
- Time(int month,int day,int hour,int minute){this->month=month;this->day=day;this->hour=hour;this->minute=minute;}
- void display();
- friend ostream & operator<<(ostream &out,const Time &);
- int getMonth(){return month;}
- int getDay(){return day;}
- int getHour(){return hour;}
- int getMinute(){return minute;}
- void setMonth(int month){this->month=month;}
- void setDay(int day){this->day=day;}
- void setHour(int hour){this->hour=hour;}
- void setMinute(int minute){this->minute=minute;}
- };
- void Time::display()
- {
- cout<<month<<" "<<day<<" "<<hour<<" "<<minute<<endl;
- }
- ostream & operator<<(ostream &out,const Time &date)
- {
- out<<date.month<<" "<<date.day<<" "<<date.hour<<" "<<date.minute<<endl;
- return out;
- }
例2:——STL
心得:
对我而言,目前重载运算符用处不是很大,能用到的地方也是重载小于号对比时间,了解咋用即可。
STL的知识太多,需要慢慢消化。
至于需要用的时候能不能想起来,还需要切实体会过才敢说啊。
想对作者说点什么? 我来说一句
STL 各种容器排序和查找算法对运算符的使用及特别注意运算符重载错误
STL排序会调用operator 例子: // 游戏中的日常任务简单例子 // 其实只要关系到使用STL进行排序的都要非常小心operator ...
- 重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:
不改变运算符的优先级不改变运算符的结合性不改变运算符所需要的操作数不能创建新的运算符
-
运算符函数可以重载为成员函数或友元函数
- 重载为成员函数,解释为:←←一元
Object . operator op ()
操作数由对象Object通过this指针隐含传递
重载为友元函数,解释为:
operator op (Object)
操作数由参数表的参数Object提供 - 重载为成员函数,解释为:←←二元
ObjectL . operator op ( ObjectR )
左操作数由ObjectL通过this指针传递,右操作数由参数ObjectR传递
重载为友元函数,解释为:
operator op ( ObjectL, ObjectR )
左右操作数都由参数传递 - STL容器的共同操作:
std::list<int> l;
以另一个容器元素为初值完成初始化:
std::list<int> l;
…
std::vector<float> c(l.begin(),l.end());
以数组元素为初值完成初始化:
int array[]={2,4,6,1345};
…
std::set<int> c(array,array+sizeof(array)/sizeof(array[0]));
与大小相关的操作(size operator):
size() //返回当前容器的元素数量
empty() //判断容器是否为空
max_size() //返回容器能容纳的最大元素数量
比较(comparison):
赋值(assignment)和交换(swap):==,!=,<,<=,>,>=比较操作两端的容器必须属于同一类型如果两个容器内的所有元素按序相等,那么这两个容器相等采用字典式顺序判断某个容器是否小于另一个容器
与迭代器(iterator)相关的操作:swap用于提高赋值操作效率
begin() //返回一个迭代器,指向第一个元素
end() //返回一个迭代器,指向最后一个元素之后
rbegin() //返回一个逆向迭代器,指向逆向遍历的第一个元素
rend() //返回一个逆向迭代器,指向逆向遍历的最后一个元素之后
元素操作
insert(pos,e) //将元素e的拷贝安插于迭代器pos所指的位置
erase(beg,end) //移除[beg,end]区间内的所有元素
clear() //移除所有元素
例子:
例1:重载<<用于输出
- class Time
- {
- private:
- int month;
- int day;
- int hour;
- int minute;
- public:
- Time():month(1),day(1),hour(0),minute(0){};
- Time(int month,int day,int hour,int minute){this->month=month;this->day=day;this->hour=hour;this->minute=minute;}
- void display();
- friend ostream & operator<<(ostream &out,const Time &);
- int getMonth(){return month;}
- int getDay(){return day;}
- int getHour(){return hour;}
- int getMinute(){return minute;}
- void setMonth(int month){this->month=month;}
- void setDay(int day){this->day=day;}
- void setHour(int hour){this->hour=hour;}
- void setMinute(int minute){this->minute=minute;}
- };
- void Time::display()
- {
- cout<<month<<" "<<day<<" "<<hour<<" "<<minute<<endl;
- }
- ostream & operator<<(ostream &out,const Time &date)
- {
- out<<date.month<<" "<<date.day<<" "<<date.hour<<" "<<date.minute<<endl;
- return out;
- }
例2:——STL
心得:
对我而言,目前重载运算符用处不是很大,能用到的地方也是重载小于号对比时间,了解咋用即可。
STL的知识太多,需要慢慢消化。
至于需要用的时候能不能想起来,还需要切实体会过才敢说啊。