手写vector
#include<iostream>
using namespace std;
template<typename T>
class myvec
{
private:
T* data;
int capacity;
int size;
public:
myvec()
{
data = NULL;
capacity = size = 0;
}
myvec(int len)
{
data = new T[len];
capacity = len;
size = 0;
}
myvec(int len,int k)
{
data = new T[len*2+1];
capacity = len*2+1;
size = len;
for (int i = 0; i < len; i++)
data[i] = k;
}
myvec(const myvec& tmp)
{
if (this == &tmp)
return;
data = new T[tmp.capacity];
capacity = tmp.capacity;
size = tmp.size;
for (int i = 0; i < size; ++i)
data[i] = tmp.data[i];
}
myvec<T> operator=(const myvec& a)
{
if (this == &a)
return *this;
delete[]data;
data = new T[a.capacity];
size = a.size;
capacity = a.capacity;
for (int i = 0; i < size; i++)
data[i] = a.data[i];
return *this;
}
T& operator[](int index)
{
if (index >= size)
cout << "Overflow!" << endl;
return data[index];
}
const myvec& push_back(const T temp)
{
if (size >= capacity)
{
T* newdata = new T[capacity * 2 + 1];
memcpy(newdata, data, capacity * sizeof(T));
delete[]data;
data = newdata;
capacity = 2 * capacity + 1;
}
data[size++] = temp;
return *this;
}
const int getsize() {
return size;
}
const int getcapaticy()
{
return capacity;
}
~myvec()
{
delete []data;
}
void pop_back()
{
if (size <= 0)
{
cout << "error!" << endl;
return;
}
size--;
}
};
int main()
{
myvec<int> vest;
myvec<int> a(10,1);
myvec<int> v (a);
myvec<int> v = a;
return 0;
}
手写string
#include<iostream>
#include<cstring>
using namespace std;
class String
{
public:
char* data;
int size;
String(const char* str)
{
if (str == NULL)
{
data = new char[1];
data[0] = '\0';
size = 0;
}
else
{
size = strlen(str);
data = new char[size + 1];
strcpy_s(data, strlen(str) + 1,str);
}
}
String(const String& str)
{
size = str.size;
data = new char[size + 1];
strcpy_s(data, str.size + 1,str.data);
}
String& operator=(const String &str)
{
if (this == &str)
return *this;
delete[]data;
size = str.size;
data = new char[size + 1];
strcpy_s(data, str.size + 1,str.data);
return *this;
}
~String()
{
delete[]data;
}
};
int main()
{
String s("this is my string");
String b(s);
String c = s;
cout << s.data << endl;
cout << b.data << endl;
cout << c.data << endl;
return 0;
}
手写shared_ptr
#include <iostream>
using namespace std;
template<typename T>
class mshared_ptr {
public:
T* ptr;
int *count;
mshared_ptr(T *_ptr)
{
ptr = _ptr;
count = new int(0);
if (ptr != NULL)
*count = 1;
}
mshared_ptr(const mshared_ptr &r)
{
cout << "拷贝构造" << endl;
ptr = r.ptr;
count = r.count;
++*r.count;
}
mshared_ptr &operator=(const mshared_ptr &r)
{
if (--*count == 0)
{
delete ptr;
delete count;
cout << "赋值重载中释放内存" << endl;
}
ptr = r.ptr;
count = r.count;
++*r.count;
return *this;
}
T operator*()
{
return *ptr;
}
~mshared_ptr()
{
if (--*count == 0)
{
delete ptr;
delete count;
}
}
int use_count()
{
return *count;
}
};
int main(void)
{
mshared_ptr<double> ptr(new double(3.14));
cout << *ptr << endl;
mshared_ptr<double> ptr1 = ptr;
cout << *ptr << endl;
cout << *ptr1 << endl;
cout << "引用计数是" << ptr.use_count() <<
"\t" << ptr1.use_count() << endl;
mshared_ptr<double> ptr_n1(new double(1.59));
ptr_n1 = ptr;
cout << "赋值给ptr_n1后引用计数是" << ptr.use_count() << endl;
}