String& operator+=(const String&);
String& operator+=(const char *);
+ String& operator+=(const std::string&);
friend String operator+(const String&, const String&);
friend String operator+(const String&, const char *);
friend String operator+(const char *, const String&);
+ friend String operator+(const String&, const std::string&);
+ friend String operator+(const std::string&, const String&);
/*
* Shifting characters out
friend std::istream& operator>>(std::istream &, String &);
/*
- * String comparison tests
+ * String comparison tests, relational operators
*/
- friend int operator==(const String& x, const String& y)
+ // String == String
+
+ friend bool operator==(const String& x, const String& y)
{
return strcmp(x.p->s, y.p->s) == 0;
}
- friend int operator!=(const String& x, const String& y)
+ friend bool operator!=(const String& x, const String& y)
{
return strcmp(x.p->s, y.p->s) != 0;
}
- friend int operator<=(const String& x, const String& y)
+ friend bool operator<=(const String& x, const String& y)
{
return strcmp(x.p->s, y.p->s) <= 0;
}
- friend int operator>=(const String& x, const String& y)
+ friend bool operator>=(const String& x, const String& y)
{
return strcmp(x.p->s, y.p->s) >= 0;
}
- friend int operator<(const String& x, const String& y)
+ friend bool operator<(const String& x, const String& y)
{
return strcmp(x.p->s, y.p->s) < 0;
}
- friend int operator>(const String& x, const String& y)
+ friend bool operator>(const String& x, const String& y)
{
return strcmp(x.p->s, y.p->s) > 0;
}
- friend int operator==(const String& x, const char * y)
+ // String == char *
+
+ friend bool operator==(const String& x, const char * y)
{
return strcmp(x.p->s, y) == 0;
}
- friend int operator!=(const String& x, const char * y)
+ friend bool operator!=(const String& x, const char * y)
{
return strcmp(x.p->s, y) != 0;
}
- friend int operator<=(const String& x, const char * y)
+ friend bool operator<=(const String& x, const char * y)
{
return strcmp(x.p->s, y) <= 0;
}
- friend int operator>=(const String& x, const char * y)
+ friend bool operator>=(const String& x, const char * y)
{
return strcmp(x.p->s, y) >= 0;
}
- friend int operator<(const String& x, const char * y)
+ friend bool operator<(const String& x, const char * y)
{
return strcmp(x.p->s, y) < 0;
}
- friend int operator>(const String& x, const char * y)
+ friend bool operator>(const String& x, const char * y)
{
return strcmp(x.p->s, y) > 0;
}
+ // char * == String
+
+ friend bool operator==(const char *y, const String& x)
+ {
+ return strcmp(y, x.p->s) == 0;
+ }
+
+ friend bool operator!=(const char *y, const String& x)
+ {
+ return strcmp(y, x.p->s) != 0;
+ }
+
+ friend bool operator<=(const char *y, const String& x)
+ {
+ return strcmp(y, x.p->s) <= 0;
+ }
+
+ friend bool operator>=(const char *y, const String& x)
+ {
+ return strcmp(y, x.p->s) >= 0;
+ }
+
+ friend bool operator<(const char *y, const String& x)
+ {
+ return strcmp(y, x.p->s) < 0;
+ }
+
+ friend bool operator>(const char *y, const String& x)
+ {
+ return strcmp(y, x.p->s) > 0;
+ }
+
+ // String == std::string
+
+ friend bool operator==(const String& x, const std::string &y)
+ {
+ return strcmp(x.p->s, y.c_str()) == 0;
+ }
+
+ friend bool operator!=(const String& x, const std::string &y)
+ {
+ return strcmp(x.p->s, y.c_str()) == 0;
+ }
+
+ friend bool operator<=(const String& x, const std::string &y)
+ {
+ return strcmp(x.p->s, y.c_str()) <= 0;
+ }
+
+ friend bool operator>=(const String& x, const std::string &y)
+ {
+ return strcmp(x.p->s, y.c_str()) >= 0;
+ }
+
+ friend bool operator<(const String& x, const std::string &y)
+ {
+ return strcmp(x.p->s, y.c_str()) < 0;
+ }
+
+ friend bool operator>(const String& x, const std::string &y)
+ {
+ return strcmp(x.p->s, y.c_str()) > 0;
+ }
+
+ // std::string == String
+
+ friend bool operator==(const std::string &y, const String& x)
+ {
+ return strcmp(y.c_str(), x.p->s) == 0;
+ }
+
+ friend bool operator!=(const std::string &y, const String& x)
+ {
+ return strcmp(y.c_str(), x.p->s) != 0;
+ }
+
+ friend bool operator>=(const std::string &y, const String& x)
+ {
+ return strcmp(y.c_str(), x.p->s) >= 0;
+ }
+
+ friend bool operator<=(const std::string &y, const String& x)
+ {
+ return strcmp(y.c_str(), x.p->s) <= 0;
+ }
+
+ friend bool operator<(const std::string &y, const String& x)
+ {
+ return strcmp(y.c_str(), x.p->s) < 0;
+ }
+
+ friend bool operator>(const std::string &y, const String& x)
+ {
+ return strcmp(y.c_str(), x.p->s) > 0;
+ }
+
/*
* Modifiers
*/
std::cout << s1 << " == " << "vwuxyz" << " = " << (s1 == "vwuxyz") << "\n";
assert((s1 == "vwuxyz") == false);
+ std::cout << "abcdef" << " == " << s1 << " = " << ("abcdef" == s1) << "\n";
+ assert(("abcdef" == s1) == true);
+
std::cout << s1 << " != " << "abcdef" << " = " << (s1 != "abcdef") << "\n";
assert((s1 != "abcdef") == false);
std::cout << s1 << " != " << "vwuxyz" << " = " << (s1 != "vwuxyz") << "\n";
assert((s1 != "vwuxyz") == true);
+ std::cout << s1 << " < " << "vwuxyz" << " = " << (s1 < "vwuxyz") << "\n";
+ assert((s1 < "vwuxyz") == true);
+ std::cout << s1 << " >= " << "vwuxyz" << " = " << (s1 <= "vwuxyz") << "\n";
+ assert((s1 >= "vwuxyz") == false);
+
+ std::cout << "vwuxyz" << " < " << s1 << " = " << ("vwuxyz" < s1) << "\n";
+ assert(("vwuxyz" < s1) == false);
+ std::cout << "vwuxyz" << " >= " << s1 << " = " << ("vwuxyz" >= s1) << "\n";
+ assert(("vwuxyz" >= s1) == true);
+
+ // Compare std::string objects
+
+ std::string s_s1("abcdef");
+ std::string s_s2("ghijkl");
+
+ std::cout << s1 << " == " << s_s1 << " = " << (s1 == s_s1) << "\n";
+ assert((s1 == s_s1) == true);
+ std::cout << s_s1 << " == " << s1 << " = " << (s_s1 == s1) << "\n";
+ assert((s_s1 == s1) == true);
+
+ std::cout << s1 << " < " << s_s2 << " = " << (s1 < s_s2) << "\n";
+ assert((s1 < s_s2) == true);
+ std::cout << s1 << " >= " << s_s2 << " = " << (s1 >= s_s2) << "\n";
+ assert((s1 >= s_s2) == false);
+
+ std::cout << s_s2 << " < " << s1 << " = " << (s_s2 < s1) << "\n";
+ assert((s_s2 < s1) == false);
+ std::cout << s_s2 << " >= " << s1 << " = " << (s_s2 >= s1) << "\n";
+ assert((s_s2 >= s1) == true);
+
// Compare character array variables