完整的string类
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char* str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80; // cin input limit
public:
// constructors and other methods
String(const char* s); // constructor
String(); // default constructor
String(const String&); // copy constructor
~String(); // destructor
int length() const { return len; }
// overloaded operator methods
//*******用于对象间赋值*******//
String& operator=(const String&);
//*******用于提高效率,不用创建临时对象,能够直接使用常规字符串类型的数据***********//
String& operator=(const char*);
//********重载中括号运算符********//
char& operator[](int i);
const char& operator[](int i) const;
// overloaded operator friends
//********字符串对象的大小比较*************//
friend bool operator<(const String& st, const String& st2);
friend bool operator>(const String& st1, const String& st2);
friend bool operator==(const String& st, const String& st2);
friend ostream& operator<<(ostream& os, const String& st);
friend istream& operator>>(istream& is, String& st);
// static function
//*********静态成员函数*************//
//静态成员函数不与某个对象相关联,它是属于某个类整体的,它只能使用静态数据成员
//公有部分声明的静态成员函数,可以使用类名和作用域解析运算符来调用。
static int HowMany();
};
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;
}
// class methods
String::String(const char* s) // construct String from C string
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
strcpy_s(str, len+1, s); // initialize pointer
num_strings++; // set object count
}
String::String() // default constructor
{
len = 4;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
//*******由于delete[]与new[]初始化的指针和空指针都兼容*************//
//所以 str = new char[1]; str[0] = '\0'; 可以修改为 str = 0;(0可以用来表示空指针,或者加(void*)说明或者用NULL也可以)
// 在C++11中, 也可以使用str = nullptr;
String::String(const String& st)
{
num_strings++; // handle static member update
len = st.len; // same length
str = new char[len + 1]; // allot space
strcpy_s(str, len + 1, st.str); // copy string to new location
}
String::~String() // necessary destructor
{
--num_strings; // required
delete[] str; // required
}
// overloaded operator methods
// assign a String to a String
String& String::operator=(const String& st)
{
if (this == &st)
return *this;
delete[] str;
len = st.len;
str = new char[len + 1];
strcpy_s(str, len + 1, st.str);
return *this;
}
// assign a C string to a String
String& String::operator=(const char* s)
{
delete[] str; //一般来说,必须释放str指向的内存,并为新字符串分配足够的内存
len = std::strlen(s);
str = new char[len + 1];
strcpy_s(str, len + 1, s);
return *this;
}
// read-write char access for non-const String
//重载中括号[]运算符,a[i]中,a是要第一个操作数,为调用函数的对象,i为第二个操作数,作为函数参数。
char& String::operator[](int i) //可以进行读/写常规对象
{
return str[i];
}
// read-only char access for const String
const char& String::operator[](int i) const //只能读取对象数据,不能修改
{
return str[i];
}
// overloaded operator friends
//由于内置的<运算符返回的就是bool值,所以这里可以直接作为bool类型的返回值
bool operator<(const String& st1, const String& st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
//这里直接调用上面写过的<运算符重载,对于内联函数来说这么写更简洁,但实际运行效率会差一些。
bool operator>(const String& st1, const String& st2)
{
return st2 < st1;
}
bool operator==(const String& st1, const String& st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
// simple String output
ostream& operator<<(ostream& os, const String& st)
{
os << st.str;
return os;
}
// quick and dirty String input
istream& operator>>(istream& is, String& st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is) //如果某种原因导致输入失败,istream对象的值将被置为false
st = temp;
while (is && is.get() != '\n')
continue;
return is;
}
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using std::cout;
using std::cin;
using std::endl;
String name;
cout << "Hi, what's your name?\n>> ";
cin >> name;
cout << name << ", please enter up to " << ArSize
<< " short sayings <empty line to quit>:\n";
String sayings[ArSize]; // array of objects
char temp[MaxLen]; // temporary string storage
int i;
for (i = 0; i < ArSize; i++)
{
cout << i + 1 << ": ";
cin.get(temp, MaxLen);
//空行检测
while (cin && cin.get() != '\n')
continue;
//空行检测
if (!cin || temp[0] == '\0') // empty line?
break; // i not incremented
else
sayings[i] = temp; // overloaded assignment
}
int total = i; // total # of lines read
if (total > 0)
{
cout << "Here are your sayings:\n";
for (i = 0; i < total; i++)
cout << sayings[i][0] << ": " << sayings[i] << endl;
int shortest = 0;
int first = 0;
for (i = 1; i < total; i++)
{
if (sayings[i].length() < sayings[shortest].length())
shortest = i;
if (sayings[i] < sayings[first])
first = i;
}
cout << "Shortest saying:\n" << sayings[shortest] << endl;;
cout << "First alphabetically:\n" << sayings[first] << endl;
cout << "This program used " << String::HowMany()
<< " String objects. Bye.\n";
}
else
cout << "No input! Bye.\n";
// keep window open
/* if (!cin)
cin.clear();
while (cin.get() != '\n')
continue; */
return 0;
}