C++ 实现vector 版本

时间:2023-09-15 12:40:32
 #include <iostream>
#include <vector>
#include <memory>
#include <thread>
#include <type_traits>
#include <typeinfo>
#include <sstream>
#include <utility> class StrVec
{
friend std::ostream &operator<<(std::ostream &os, const StrVec &rhs); private:
std::string *elemnets_;
std::string *memry_free;
std::string *cap;
std::allocator<std::string> alloc;//为所有StrVec分配内存 //申请2倍范围空间,并把范围移动到新空间
std::pair<std::string *, std::string *> alloc_n_copy(const std::string *beg, const std::string *end)
{
auto new_memry = alloc.allocate(end - beg);
return {new_memry, std::uninitialized_copy(beg, end, new_memry)};
}; void free() //释放目前申请的内存
{
for (auto ptr = memry_free; ptr != elemnets_;)
{
alloc.destroy(--ptr);
} alloc.deallocate(elemnets_, memry_free - elemnets_);
} void realloctor() //重新申请大于目前2倍空间;
{
auto newcapacity = size() ? * size() : ;
auto newmemery = alloc.allocate(newcapacity);
auto dest = newmemery;
auto elem = elemnets_;//指向当前对象的头
for (size_t i = ; i != size(); ++i)
{
//move会让elem指向的string对象放弃自己的内存管理权并返回,然后construct使用string的移动构造函数构建dest指向的地址
alloc.construct(dest++, std::move(*elem++));
}
free();
elemnets_ = newmemery;
memry_free = dest;
cap = elemnets_ + newcapacity;
}; void Chk_n_alloc()
{
if (size() == capacity())
realloctor();
} public:
StrVec() : elemnets_(nullptr), memry_free(nullptr), cap(nullptr)
{} StrVec(std::initializer_list<std::string> li)
{
auto newadress = alloc_n_copy(li.begin(), li.end());
elemnets_ = newadress.first;
cap = memry_free = newadress.second;
} //只是构造(每次想着释放- -)
StrVec(const StrVec &rhs)
{
auto newadress = alloc_n_copy(rhs.begin(), rhs.end());
elemnets_ = newadress.first;
cap = memry_free = newadress.second;
} StrVec(StrVec &&rhs)
: elemnets_(rhs.elemnets_), memry_free(rhs.memry_free), cap(rhs.cap)
{
//置空
rhs.elemnets_ = rhs.cap = rhs.memry_free = nullptr;
} StrVec &operator=(const StrVec &rhs)
{
if (&rhs != this)
{
auto newadress = alloc_n_copy(rhs.begin(), rhs.end());
free();
elemnets_ = newadress.first;
memry_free = cap = newadress.second;
}
return *this;
} StrVec &operator=(StrVec &&rhs)
{
if (&rhs != this)
{
elemnets_ = rhs.elemnets_;
memry_free = rhs.memry_free;
cap = rhs.cap;
rhs.elemnets_ = rhs.cap = rhs.memry_free = nullptr;
}
return *this;
} //列表赋值初始化
StrVec &operator=(std::initializer_list<std::string> li)
{
auto newadress = alloc_n_copy(li.begin(), li.end());
free();
elemnets_ = newadress.first;
memry_free = cap = newadress.second; return *this;
} std::string &operator[](std::size_t size_)
{
return elemnets_[size_];
} std::string &operator[](std::size_t size) const
{
return elemnets_[size]; } bool operator==(const StrVec &s)
{
if (size() != s.size())
return false;
auto it = elemnets_, its = s.elemnets_;
while (it != memry_free)
{
if (*it++ != *its++)
return false;
}
return true;
} bool operator<(const StrVec &rhs)
{
if (this->size() < rhs.size())
{
return true;
} else if (this->size() > rhs.size())
{
return false;
} auto rhs_elemte = rhs.elemnets_;
for (auto iter_ptr = elemnets_; iter_ptr != memry_free;)
{
if (*iter_ptr++ > *rhs_elemte++)
{
return false;
} else
{
return true;
}
}
return false;
} bool operator>(const StrVec &rhs)
{
return !(*this < rhs) && (this != &rhs);
} bool operator!=(const StrVec &rhs)
{
return !(*this == rhs);
} public:
template<typename ...Args>
void emplace_back(Args &&... paracage)
{
Chk_n_alloc();
alloc.construct(memry_free++, std::forward<Args>(paracage)...);
} void push_back(const std::string &s)
{
Chk_n_alloc();//确保空间剩余
alloc.construct(memry_free++, s);//在尾后构建一个s(s的拷贝构造函数构造),并把尾后指针first_free指向下一个
} void pop_back()
{
if (memry_free != elemnets_)
{
alloc.destroy(--memry_free);
}
} std::size_t size() const
{
return memry_free - elemnets_;
} std::size_t capacity() const
{
return cap - elemnets_;
} std::string *begin() const
{
return elemnets_;
} std::string *end() const
{
return memry_free;
}
}; std::ostream &operator<<(std::ostream &os, const StrVec &rhs)
{
for (auto ptr = rhs.elemnets_; ptr != rhs.memry_free;)
{
os << *ptr++ << "\n";
} return os;
} int main(int argc, char *argv[])
{ StrVec strvec{"Hello", "World", "this", "std::string", "vector<std::string>"};
std::cout << strvec;
std::cout << strvec[] << std::endl;
strvec.push_back("你好");
std::cout << strvec[] << std::endl; std::cout << "------------" << std::endl;
std::cout << strvec;
strvec.pop_back();
strvec.pop_back();
strvec.pop_back();
strvec.pop_back();
std::cout << "------------" << std::endl;
std::cout<<strvec;
std::cout << "------------" << std::endl;
strvec.emplace_back("其实emeplace通过参数包转发给std::string的构造");
strvec.emplace_back(,'c');
std::cout<<strvec; return ;
}