知识点

  • 重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:
不改变运算符的优先级
不改变运算符的结合性
不改变运算符所需要的操作数 
不能创建新的运算符
  • 运算符函数可以重载为成员函数或友元函数
  • 重载为成员函数,解释为:←←一元
    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):
swap用于提高赋值操作效率
与迭代器(iterator)相关的操作:
begin() //返回一个迭代器,指向第一个元素
end() //返回一个迭代器,指向最后一个元素之后
rbegin() //返回一个逆向迭代器,指向逆向遍历的第一个元素
rend() //返回一个逆向迭代器,指向逆向遍历的最后一个元素之后
元素操作
insert(pos,e) //将元素e的拷贝安插于迭代器pos所指的位置
erase(beg,end) //移除[beg,end]区间内的所有元素

clear() //移除所有元素

例子:

例1:重载<<用于输出

[cpp] view plain copy
  1. class Time  
  2. {  
  3. private:  
  4.     int month;  
  5.     int day;  
  6.     int hour;  
  7.     int minute;  
  8. public:  
  9.     Time():month(1),day(1),hour(0),minute(0){};  
  10.     Time(int month,int day,int hour,int minute){this->month=month;this->day=day;this->hour=hour;this->minute=minute;}  
  11.     void display();  
  12.     friend ostream & operator<<(ostream &out,const Time &);  
  13.     int getMonth(){return month;}  
  14.     int getDay(){return day;}  
  15.     int getHour(){return hour;}  
  16.     int getMinute(){return  minute;}  
  17.     void setMonth(int month){this->month=month;}  
  18.     void setDay(int day){this->day=day;}  
  19.     void setHour(int  hour){this->hour=hour;}  
  20.     void setMinute(int  minute){this->minute=minute;}  
  21. };  
  22. void Time::display()  
  23. {  
  24.     cout<<month<<" "<<day<<" "<<hour<<" "<<minute<<endl;  
  25. }  
  26. ostream & operator<<(ostream &out,const Time &date)  
  27. {  
  28.     out<<date.month<<" "<<date.day<<" "<<date.hour<<" "<<date.minute<<endl;  
  29.     return out;  
  30. }  

例2:——STL

重载运算符和STL总结

心得:

对我而言,目前重载运算符用处不是很大,能用到的地方也是重载小于号对比时间,了解咋用即可。

STL的知识太多,需要慢慢消化。

至于需要用的时候能不能想起来,还需要切实体会过才敢说啊。

个人分类: C++需要被检查
想对作者说点什么? 我来说一句
  • 重载运算符函数可以对运算符作出新的解释,但原有基本语义不变:
不改变运算符的优先级
不改变运算符的结合性
不改变运算符所需要的操作数 
不能创建新的运算符
  • 运算符函数可以重载为成员函数或友元函数
  • 重载为成员函数,解释为:←←一元
    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):
swap用于提高赋值操作效率
与迭代器(iterator)相关的操作:
begin() //返回一个迭代器,指向第一个元素
end() //返回一个迭代器,指向最后一个元素之后
rbegin() //返回一个逆向迭代器,指向逆向遍历的第一个元素
rend() //返回一个逆向迭代器,指向逆向遍历的最后一个元素之后
元素操作
insert(pos,e) //将元素e的拷贝安插于迭代器pos所指的位置
erase(beg,end) //移除[beg,end]区间内的所有元素

clear() //移除所有元素

例子:

例1:重载<<用于输出

[cpp] view plain copy
  1. class Time  
  2. {  
  3. private:  
  4.     int month;  
  5.     int day;  
  6.     int hour;  
  7.     int minute;  
  8. public:  
  9.     Time():month(1),day(1),hour(0),minute(0){};  
  10.     Time(int month,int day,int hour,int minute){this->month=month;this->day=day;this->hour=hour;this->minute=minute;}  
  11.     void display();  
  12.     friend ostream & operator<<(ostream &out,const Time &);  
  13.     int getMonth(){return month;}  
  14.     int getDay(){return day;}  
  15.     int getHour(){return hour;}  
  16.     int getMinute(){return  minute;}  
  17.     void setMonth(int month){this->month=month;}  
  18.     void setDay(int day){this->day=day;}  
  19.     void setHour(int  hour){this->hour=hour;}  
  20.     void setMinute(int  minute){this->minute=minute;}  
  21. };  
  22. void Time::display()  
  23. {  
  24.     cout<<month<<" "<<day<<" "<<hour<<" "<<minute<<endl;  
  25. }  
  26. ostream & operator<<(ostream &out,const Time &date)  
  27. {  
  28.     out<<date.month<<" "<<date.day<<" "<<date.hour<<" "<<date.minute<<endl;  
  29.     return out;  
  30. }  

例2:——STL

重载运算符和STL总结

心得:

对我而言,目前重载运算符用处不是很大,能用到的地方也是重载小于号对比时间,了解咋用即可。

STL的知识太多,需要慢慢消化。

至于需要用的时候能不能想起来,还需要切实体会过才敢说啊。

相关文章: