另外:operator() 、operator== 在程序中是究竟是怎樣被調(diào)用的#include <iostream> #include <set> using namespace std; template <class T> class RuntimeCmp { public: enum cmp_mode {normal, reverse}; private: cmp_mode mode; public: //constructor for sorting criterion //-default criterion uses value normal RuntimeCmp (cmp_mode m=normal) : mode(m) { } //comparision of elements bool operator() (const T& t1, const T& t2) const { return mode == normal ? t1 < t2 : t2 < t1; } //comparision of sorting criteria bool operator== (const RuntimeCmp& rc) { return mode == rc.mode; } }; //type of a set that uses this sorting criterion typedef set<int,RuntimeCmp<int> > IntSet; void fill (IntSet& Set) { //fill insert elements in random order Set.insert(4); Set.insert(7); Set.insert(5); Set.insert(1); Set.insert(6); Set.insert(2); Set.insert(5); } template <class T> int main() { //create, fill, and print set with normal element order //-uses default sorting criterion IntSet coll1; fill(coll1); copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));cout<<endl;RuntimeCmp<int> reverse_order(RuntimeCmp<int>::reverse); //create, fill, and print set with reverse element order IntSet coll2(reverse_order); fill(coll2); copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));cout<<endl;//assign elements AND sorting criterion coll1 = coll2; coll1.insert(3); // PRINT_ELEMENTS coll1copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));cout<<endl;//just to make sure... if (coll1.value_comp() == coll2.value_comp()) { cout << "coll1 and coll2 have same sorting criterion" << endl; } else { cout << "coll1 and coll2 have different sorting criterion" << endl; } return 0;}
2 回答

達(dá)令說(shuō)
TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
根據(jù)你的出錯(cuò)信息來(lái)看,要么你沒(méi)有main,要么你有多余的main
這些運(yùn)算符的調(diào)用和函數(shù)是一樣的
他們有參數(shù),也有返回類型,當(dāng)然并不是所有的都是這樣,如 operator type()就沒(méi)有返回類型,其實(shí)type就是,調(diào)用我也不太清楚.如class operator+(),如是全局的則()內(nèi)的一個(gè)是左操作數(shù),一個(gè)是右操作數(shù) ,class代表返回值,operator==類似
- 2 回答
- 0 關(guān)注
- 160 瀏覽