#include"Vec.h"
#include<iterator>
class Str
{
//输入输出
friend std::istream& operator >> (std::istream&, Str&);
public:
typedef Vec<char>::size_type size_type;
Str() {};//默认构造函数,构建空的str
//生成一个Str对象,包含c的n个复件
Str(size_type n, char c) :data(n, c) {};
//生成一个Str对象并用一个空字符结尾的字符数组来初始化
Str(const char* cp)
{ //把参数提供的字符加到data对象里
std::copy(cp,cp+std::strlen(cp),std::back_inserter(data));
}
template<class In>
Str(In b, In e)
{ //把[b,e)之间的内容加到data后面
std::copy(b,e,std::back_inserter(data));
}
//[]重载
char& operator[](size_type i) { return data[i]; }
const char& operator[](size_type i) const { return data[i]; }
Str& operator+=(const Str& s)
{
std::copy(s.data.begin(),s.data.end(),std::back_inserter(data));
return *this;
}
size_type size() const { return data.size(); }
private:
Vec<char> data;
};
std::ostream& operator<<(std::ostream&, const Str&);
Str operator+(const Str&,const Str&);
//输出运算符
std::ostream& operator<<(std::ostream& os, const Str& s)
{
for (Str::size_type i = 0; i!=s.size(); ++i)
{
os << s[i];
}
return os;
}
std::istream& operator >> (std::istream& is,Str& s)
{
//抹去存在的值
s.data.clear();
//按顺序读字符并忽略前面的空格字符
char c;
while (is.get(c) && isspace(c));
if (is)
{
do
{
s.data.push_back(c);
} while (is.get(c)&&!isspace(c));
}
return is;
}
inline Str operator+(const Str & s, const Str & t)
{
Str r = s;
r += t;
return r;
}