// chapter-14.cpp : 重載運(yùn)算符和類型轉(zhuǎn)換(operator int()()const)
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<map>
#include<set>
#include<functional>
#include<algorithm>
#include<numeric>
using namespace std;
class Example_operator
{
public:
Example_operator() = default;
Example_operator(const string &s, const map<string, set<string>> &m) :id(s), information(m) {}
friend ostream& operator<<(ostream &os,const Example_operator &rhs);
friend istream& operator>>(istream &in, Example_operator &rhs);
Example_operator operator+=(const Example_operator &rhs);
friend Example_operator operator+(const Example_operator &lhs, const Example_operator &rhs);
friend bool operator==(const Example_operator &lhs, const Example_operator &rhs);
friend bool operator!=(const Example_operator &lhs, const Example_operator &rhs);
private:
string id;
map<string, set<string>> information;
//void read_from_input();
};
//輸入輸出運(yùn)算符,必須是非成員函數(shù)乳讥。const引用,不改變內(nèi)容節(jié)省時間!
ostream& operator<<(ostream &os, const Example_operator &rhs)
{
os << rhs.id <<":"<< endl;
for (auto &r : rhs.information)
{
cout << "\b" << r.first << " : ";
for (auto &&rs : r.second)
{
cout << rs << " ";
}
cout << endl;
}
return os;
}
istream& operator>>(istream &in, Example_operator &rhs)
{
cout << "Please input the name:" << endl;
in >> rhs.id;
cout << "Please input something(keys value ...(; to stop!)),or q to quit..." << endl;
if (in) //檢查輸入是否成功余耽!輸入運(yùn)算符必須檢查輸入失敗,而輸出則不需要笋额!
//當(dāng)出現(xiàn)輸入錯誤是冯袍,最好是自己使用IO標(biāo)準(zhǔn)庫來表示這些錯誤!
{
string key, value;
while (cin >> key)
{
if (key == "q")
break;
else
{
set<string> tmp_set;
while (cin >> value)
{
if (value == ";")
break;
tmp_set.insert(value);
}
rhs.information[key] = tmp_set;
}
}
}
return in;
}
Example_operator Example_operator::operator+=(const Example_operator &rhs)
{
Example_operator &sum = *this;
if (sum.id == rhs.id)
{
for (auto &r : sum.information)
{
for (auto &k : rhs.information)
{
if (r.first == k.first)
{
r.second.insert(k.second.cbegin(), k.second.cend());
}
else
{
sum.information.insert(k);
}
}
}
}
return sum;
}
Example_operator operator+(const Example_operator &lhs, const Example_operator &rhs)
{
Example_operator sum = lhs;
if (sum.id == rhs.id)
{
sum += rhs;
}
return sum;
}
bool operator==(const Example_operator &lhs, const Example_operator &rhs)
{
return (lhs.id == rhs.id && lhs.information == rhs.information);
}
bool operator!=(const Example_operator &lhs, const Example_operator &rhs)
{
return !(lhs == rhs);
}
class PrintString //如果類定義了函數(shù)調(diào)用運(yùn)算符苍鲜,就可以像函數(shù)一樣使用該對象思灰!
{
public:
PrintString(ostream &o = cout, char c = ' ') :os(o), sep(c) {}
void operator()(const string &s) const { os << s << sep; }
private:
ostream &os;
char sep;
};
class SmallInt//構(gòu)造函數(shù)將算術(shù)類型的值轉(zhuǎn)換成smallInt對象,而類型轉(zhuǎn)換運(yùn)算符將smallInt對象轉(zhuǎn)換成int混滔!
{
public:
SmallInt(int i = 0) :val(i)
{
if (i < 0 || i>255)
throw out_of_range("Bad smallint value!");
}
operator int()const { return val; }//類型轉(zhuǎn)換H骶巍(包含數(shù)組指針和函數(shù)指針)
private:
size_t val;
};
class SmallInt_Explicit//顯示類型轉(zhuǎn)換,必須使用static_cast<int>強(qiáng)制轉(zhuǎn)換坯屿!若為條件油湖,則同樣允許自動轉(zhuǎn)換!
{
public:
SmallInt_Explicit(int i = 0) :val(i)
{
if (i < 0 || i>255)
throw out_of_range("Bad smallint value!");
}
explicit operator int()const { return val; }//定義類型轉(zhuǎn)換领跛!
private:
size_t val;
};
int main()
{
Example_operator stu01("xiaoming", { { "grade",{ "99" } },{ "car",{ "baoma" } } });
cout << stu01 << endl;
Example_operator stu02;
cin >> stu02;
cout << stu02 << endl;
cout << (stu01 + stu02) << endl;
PrintString printer;
printer("abc"); //for_each(v.begin(),v.end(),printer(cout,'\n'))7Φ隆;lambda是函數(shù)對象吠昭,編譯器編譯成一個未命名類的未命名對象喊括!如果含有值捕獲,則對應(yīng)類將建有對應(yīng)的數(shù)據(jù)成員矢棚!
//標(biāo)準(zhǔn)庫自帶函數(shù)對象郑什,定義在頭文件functional中。主要應(yīng)用于泛型算法中蒲肋!
//算術(shù):plus<Type> minus multiplies divides modulus negate
//關(guān)系:equal_to not_equal_to greater greater_equal less less_equal
//邏輯:logical_and logical_or logical_ not
//plus<int> intadd;int sum=intadd(3,4)
//函數(shù)蘑拯、函數(shù)指針劫拢、lambda表達(dá)式、bind創(chuàng)建對象强胰、重載運(yùn)算符舱沧,均為可調(diào)用對象!可調(diào)用對象也有類型偶洋,為調(diào)用形式熟吏!int(int,int),接受2個int玄窝,返回一個int牵寺!
//使用function儲存可調(diào)用對象,定義在頭文件functional中恩脂!
//function<T> f; f是一個儲存可調(diào)用對象帽氓!
//function<T> f(nullptr)
//function<T> f(obj)
//f 將f作為條件,當(dāng)f含有一個可調(diào)用對象為真俩块!
//f(args) 調(diào)用f中的對象黎休,參數(shù)為args
//result_type 返回可調(diào)用對象返回的類型
//argument_type 返回實參類型!
//first_argument_type
//second_argument_type
// function<int(int, int)> f1 = add; 函數(shù)指針
// function<int(int, int)> f2 = divide(); 函數(shù)對象
function<int(int, int)> f3 = [](int i, int j) {return i*j; };
//map<string,function<int(int,int)>> binops={{"+",add}};
//傳入重載函數(shù)時玉凯,為了避免二義性势腮,1、int (*fp)(int,int)=add;2漫仆、lambad
//自定義類型轉(zhuǎn)換捎拯,返回類型必須能作為函數(shù)的返回類型!(數(shù)組和函數(shù)不能返回盲厌,但其指針可以)
SmallInt si;
si = 4; //轉(zhuǎn)為size_t
cout << si + 3 << endl; //size_t轉(zhuǎn)為int署照,再執(zhí)行整數(shù)的加法!
//實際中很少定義類型轉(zhuǎn)換符吗浩,但是定義向bool的類型轉(zhuǎn)換還是比較普遍的現(xiàn)象建芙!
//為避免隱式類型轉(zhuǎn)換引發(fā)錯誤結(jié)果,進(jìn)行顯示的類型轉(zhuǎn)換運(yùn)算符定義拓萌!
SmallInt_Explicit sie = 3;
cout << static_cast<int>(sie) + 4 << endl; //當(dāng)表達(dá)式被用作條件岁钓,顯示類型轉(zhuǎn)換會被隱式的執(zhí)行!
//避免二義性類型轉(zhuǎn)換:1微王、f(b)=A(b)和b.operator(A)屡限,避免同時定義相同的類型轉(zhuǎn)換!(構(gòu)造函數(shù)和成員函數(shù)炕倘、同類避免即轉(zhuǎn)int钧大,又轉(zhuǎn)double)2、除了轉(zhuǎn)換為bool罩旋,應(yīng)該避免定義類型轉(zhuǎn)換啊央!
return 0;
}
//為自定義類對象定義重載運(yùn)算符眶诈,有利于編寫和閱讀!(內(nèi)置類型無法重載!)
//除了operator()外瓜饥,其他重載運(yùn)算符不能有默認(rèn)實參J徘恕?乓土?宪潮?
//不能重載的運(yùn)算符《:: .* . ?:》/可以重載的運(yùn)算符《+ - * /(算術(shù)) &(比較) <<(IO) [](下標(biāo)) ()(函數(shù)調(diào)用運(yùn)算符 int operator()(int val) const{returnval<0?-val:val}) -> &&(邏輯) new》
//重載運(yùn)算符應(yīng)該與內(nèi)置類型一致,有operator==則應(yīng)該有operator!=趣苏,有operator<則應(yīng)該有其他比較關(guān)系狡相,邏輯和關(guān)系返回布爾值,算術(shù)返回類類型食磕,賦值返回左側(cè)對象的引用尽棕!
//具有對稱性的運(yùn)算符應(yīng)該定義為非成員函數(shù)!如算術(shù)彬伦、相等滔悉、關(guān)系和位運(yùn)算符!;復(fù)合賦值運(yùn)算符應(yīng)該是成員函數(shù)媚朦!
chapter-14
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來资厉,“玉大人厅缺,你說我怎么就攤上這事⊙绯ィ” “怎么了湘捎?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長窄刘。 經(jīng)常有香客問我窥妇,道長,這世上最難降的妖魔是什么娩践? 我笑而不...
- 正文 為了忘掉前任活翩,我火速辦了婚禮烹骨,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘材泄。我一直安慰自己沮焕,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布拉宗。 她就那樣靜靜地躺著遇汞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪簿废。 梳的紋絲不亂的頭發(fā)上空入,一...
- 文/蒼蘭香墨 我猛地睜開眼扫尖,長吁一口氣:“原來是場噩夢啊……” “哼白对!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起换怖,我...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年铸屉,在試婚紗的時候發(fā)現(xiàn)自己被綠了钉蒲。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站怠益,受9級特大地震影響仪搔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蜻牢,卻給世界環(huán)境...
- 文/蒙蒙 一烤咧、第九天 我趴在偏房一處隱蔽的房頂上張望偏陪。 院中可真熱鬧,春花似錦煮嫌、人聲如沸笛谦。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽饥脑。三九已至,卻和暖如春懦冰,著一層夾襖步出監(jiān)牢的瞬間灶轰,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓内地,卻偏偏與公主長得像伴澄,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子阱缓,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 回應(yīng)非凌!互動,團(tuán)隊荆针,共情敞嗡。青松嶺~趕車萬山大叔。 戚繼光~選兵祭犯,正經(jīng)史秸妥,浙江金華工人階級,礦工沃粗,長劍劍法。農(nóng)民階級键畴,...
- 申論閱卷絕密!!! 陪你一起去公考 漫漫公考路上惹想,很多人都會覺得申論讓人捉摸不透 每一次做完題都不知道自己哪有問題...
- 01 幾個月前嘀粱,跟媽視頻時她冷不丁地說:“小邵的媽媽去世了激挪〕浇疲”而我也冷不丁地愣住了。 小邵是我的小學(xué)同學(xué)垄分,和我同住...