Effective STL 19 understand the difference between equality and equivalence
2017-08-27 18:48
381 查看
Equivalence is defined in terms of comparison function(!(w1 < w2) && !(w2 < w1)), so clients of a standard associative container need to only one comparison function.
set wants a comparison function type, not an actual function. To bridge this gap, we write a functor class whose operator() calls ciStringCompare:
ciStringCompare
set wants a comparison function type, not an actual function. To bridge this gap, we write a functor class whose operator() calls ciStringCompare:
ciStringCompare
int ciCharCompare(char c1, char c2) { // in both C and C++, char may or may not be signed, the only way to // ensure that its value is representable as an unsigned char is to cast int lc1 = tolower(static_cast<unsigned char>(c1)); int lc2 = tolower(static_cast<unsigned char>(c2)); if (lc1 < lc2) return -1; if (lc1 > lc2) return 1; return 0; } int ciStringCompareLmpl(const string& s1, const string& s2) { typedef pair<string::const_iterator, string::const_iterator> PSCI; // mismatch will stop when the predicate returns false PSCI p = mismatch(s1.begin(), s1.end(), s2.begin(), not2(ptr_fun(ciCharCompare))); // s1 is short than s2 if (p.first == s1.end) { if (p.second == s2.end) return 0; else return -1; } return ciCharCompare(*p.first, *p.second); } int ciStringCompare(const string& s1, const string& s2) { if (s1.size() <= s2.size()) return ciCharCompare(s1, s2); else return ciCharCompare(s2, s1); }
struct CIStringCompare: // also see item 40 public binary_function<string, string, bool> { bool operator()(const string& lhs, const string& rhs) const { return ciStringCompare(lhs, rhs); // also see item 35 } }; set<string, CIStringCompare> ciss; ciss.insert("Persephon"); ciss.insert("persephon"); // no new element is added to the set // the standard associative constainers are kept in sorted order, so each container // must have a comparsion function. Equivalence is defined in terms of this comparison function. if (ciss.find("persephone") != ciss.end()) ... // this test will succeed // equivalence if (find(ciss.begin(), ciss.end(), "persephone") // fail // equality // string("persephon") != string("Persephon")
相关文章推荐
- Understand the difference between IN and EXISTS in subqueries
- What is the Difference Between http and https?
- The Difference Between Keypoints and Descriptors
- The difference between declaring a variable “id” and “NSObject *”
- What's the difference between CUDA shared and global memory?
- The difference between LayoutInflater.inflate and findViewById
- test the difference between "DEFAULT NULL" and "DEFAULT 0"
- What is the difference between a pack slip and a pick slip?
- What’s The Difference Between A Codec, Container And Video Format?
- the difference between Oracle and Mysql
- Q & A: The difference between hard and soft links
- What is the difference between ++i and i++
- What Is The Difference Between Artificial Intelligence And Machine Learning?
- What is the difference between the mouseover and mouseenter events?
- What is the difference between using a script and an animation?
- What’s the Difference Between Artificial Intelligence, Machine Learning, and Deep Learning?
- The difference between Asp.Webpart and WSS.Webpart baseclass
- The differences between Carriage Return(CR) and Line Feed(LF)
- what is the difference between ELF files and bin files
- What is the difference between error active and error passive?