sorting - c++ sort vector of template <char*, char*> objects -


i need implement code works:

pair<char*, char*> p1, p2, p3; vector<pair<char*, char*> > v; cin >> p1 >> p2 >> p3; v.push_back(p1); v.push_back(p2); v.push_back(p3); sort(v.begin(), v.end());  (it = v.begin(); != v.end(); ++it){     cout << *it << endl; } 

i made template class, tested operators , works fine, need make specialization class upper code works. here did:

template<typename t1, typename t2> class pair { t1 first; t2 second;  public:     pair(const t1& t1=0, const t2& t2=0) : first(t1), second(t2){}     pair(const pair<t1, t2>& other) : first(other.first), second(other.second){}      bool operator==(const pair<t1, t2>& other) const{         return first == other.first && second == other.second;     }      bool operator!=(const pair<t1, t2>& other) const{         return first != other.first && second != other.second;     }      bool operator<(const pair<t1, t2>& other) const{         return first < other.first && second < other.second;     }      bool operator<=(const pair<t1, t2>& other) const{         return first <= other.first && second <= other.second;     }      bool operator>(const pair<t1, t2>& other) const{         return first > other.first && second > other.second;     }      bool operator>=(const pair<t1, t2>& other) const{         return first >= other.first && second >= other.second;     }      pair<t1, t2>& operator=(const pair<t1, t2>& other){         first=other.first;         second=other.second;         return *this;     }      friend ostream& operator<<(ostream& os, const pair<t1, t2>& b){         os << "values: " << b.first << " " << b.second << endl;         return os ;     }      friend istream& operator>>(istream& is, pair<t1, t2>& p){         >> p.first >> p.second;         return is;     }      friend void swap(pair<t1, t2>& a, pair<t1, t2>& b){         pair<t1, t2> t = a;         = b;         b = t;     } }; 

i know specialization need write code separately, did this:

template<> class pair<char*, char*>{ char s1, s2; public:     pair(const char t1='x', const char t2='y') : s1(t1), s2(t2){}      friend ostream& operator<<(ostream& os, const pair<char*, char*>& b){         os << "values: " << b.s1 << " " << b.s2 << endl;         return os ;     }      friend istream& operator>>(istream& is, pair<char*, char*>& p){         >> p.s1 >> p.s2;         return is;     }      friend void sort(){         // ???     } }; 

operators << , >> works fine, stuck on sort because have no idea how implement sort method works , sorts alphabetically letters in specialization template object. googled, not there, hints or appreciated...

your specialization inconsistent general case.

the template<t1, t2> class pair; contain member of type t1 , member of type t2.

so, consistent, template<char *, char *> class pair; should contain 2 char * (and allocate/deallocate it, suppose). instead, specialization contain 2 char. naive.

what intentions? crete pair char * (c-style strings) or pair single char characters?

as is, , observed bo persson, relational operators (in generic class) bad. wanting ignore (imho) better develop relational operators friend , not class methods, it's important if result true a == b, a != b result false , conversely. otherwise, std::sort() can crash program.

look @ operator==() , operator!=() implementations

bool operator==(const pair<t1, t2>& other) const{     return first == other.first && second == other.second; }  bool operator!=(const pair<t1, t2>& other) const{     return first != other.first && second != other.second; } 

consider case first == other.first , second != other.second; result both operator returns false. dangerous; dangerous.

same problem other relational operator.

consider case first < other.first , second > other.second; 4 other operators (<, <=, >, >=) return false.

i suggest write relational operator in inter-dependent way; like

template <typename t1, typename t2>    bool operator== (const pair<t1, t2> & p1, const pair<t1, t2> & p2)     { return (p1.first == p2.first) && (p1.second == p2.second); }  template <typename t1, typename t2>    bool operator!= (const pair<t1, t2> & p1, const pair<t1, t2> & p2)     { return ! (p1 == p2); }  template <typename t1, typename t2>    bool operator< (const pair<t1, t2> & p1, const pair<t1, t2> & p2)     { return    (p1.first < p2.first)               || ((p1.first == p2.first) && (p1.second < p2.second)); }  template <typename t1, typename t2>    bool operator<= (const pair<t1, t2> & p1, const pair<t1, t2> & p2)     { return ! (p2 < p1); }  template <typename t1, typename t2>    bool operator> (const pair<t1, t2> & p1, const pair<t1, t2> & p2)     { return (p2 < p1); }  template <typename t1, typename t2>    bool operator>= (const pair<t1, t2> & p1, const pair<t1, t2> & p2)     { return ! (p1 < p2); } 

remembering declare friend in classes, operator==() , operator<().

p.s.: caution, example code not tested.

p.s.2: sorry bad english.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -