64,646
社区成员
发帖
与我相关
我的任务
分享
#ifndef GUARD_Vec_h
#define GUARD_Vec_h
#include <memory>
#include <cstddef>
template <class T> class Vec {
public:
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef std::ptrdiff_t difference_type; //ptrdiff_t <cstddef>
Vec() { create(); }
explicit Vec(size_type n, const T& t = T()) { create(n, t); }
Vec(const Vec& v) { create(v.begin(), v.end()); }
Vec& operator=(const Vec&); // 单独定义
~Vec() { uncreate(); }
void clear() { uncreate(); }
T& operator[](size_type i) { return data[i]; }
const T& operator[](size_type i) const { return data[i]; }
void push_back(const T& t) {
if (avail == limit)
grow();
unchecked_append(t);
}
size_type size() const { return avail - data; }
iterator begin() { return data; }
const_iterator begin() const { return data; }
iterator end() { return avail; }
const_iterator end() const { return avail; }
private:
iterator data;
iterator avail;
iterator limit;
std::allocator<T> alloc;
void create();
void create(size_type, const T&);
void create(const_iterator, const_iterator);
void uncreate();
void grow();
void unchecked_append(const T&);
};
#endif
#include <memory>
#include <cstddef>
#include "Vec.h"
template <class T>
Vec<T>& Vec<T>::operator=(const Vec& rhs)
{
if (&rhs != this) {
uncreate();
create(rhs.begin(), rhs.end());
}
return *this;
}
template <class T> void Vec<T>::create()
{
data = avail = limit = 0;
}
template <class T> void Vec<T>::create(size_type n, const T& val)
{
data = alloc.allocate(n);
limit = avail = data + n;
uninitialized_fill(data, limit, val);
}
template <class T>
void Vec<T>::create(const_iterator i, const_iterator j)
{
data = alloc.allocate(j - i);
limit = avail = uninitialized_copy(i, j, data);
}
template <class T> void Vec<T>::uncreate()
{
if (data) {
iterator it = avail;
while (it != data)
alloc.destroy(--it);
alloc.deallocate(data, limit - data);
}
data = limit = avail = 0;
}
template <class T> void Vec<T>::grow()
{
//when growing, allocate twice as much space as currently in use
size_type new_size = max(2 * (limit - data), ptrdiff_t(2));
iterator new_data = alloc.allocate(new_size);
iterator new_avail = uninitialized_copy(data, avail, new_data);
uncreate();
data = new_data;
avail = new_avail;
limit = data + new_size;
}
template <class T> void Vec<T>::unchecked_append(const T& val)
{
alloc.construct(avail++, val);
}
#include "Vec.h"
#include <algorithm>
#include <iterator>
#include <cstring>
class Str {
//class 的{和第一个保护标签之间的成员都是private的
friend std::istream& operator>>(std::istream&, Str&);
public:
Str& operator+=(const Str& s) {
std::copy(s.data.begin(), s.data.end(), std::back_inserter(data));
return *this;
}
typedef Vec<char>::size_type size_type;
Str() {}
Str(size_type n, char c) : data(n, c) {}
Str(const char* cp) {
std::copy(cp, cp + strlen(cp), std::back_inserter(data));
}
template <class In> Str(In b, In e) {
std::copy(b, e, std::back_inserter(data));
}
size_type size() const { return data.size(); }
char& operator[](size_type i) { return data[i]; }
const char& operator[](size_type i) const { return data[i]; }
private:
Vec<char> data;
};
std::ostream& operator<<(std::ostream&, const Str&);
Str operator+(const Str& s, const Str& t);
#include "Str.h"
#include "Vec.h"
using namespace std;
ostream& operator<<(ostream& os, const Str& s)
{
for (Str::size_type i = 0; i != s.size(); ++i)
os << s[i];
return os;
}
istream& operator>>(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));
if (is)
is.unget();
}
return is;
}
Str operator+(const Str& s, const Str& t)
{
Str r = s;
r += t;
return r;
}
int main()
{}