• C++模板与STL(二):STL概念仿真


    目录

    1.Vector容器仿真

    1.1 定义内嵌类型表

    1.2 定义相关属性

    1.3 push_back()

    1.4 back() 

    1.5 下标操作[]

    1.6 总体代码与测试案例

    2.List仿真

    2.1 定义结点

    2.2 设计迭代器(将类型表与迭代器整合)

    2.2.1 重载++

    2.2.2 重载--

    2.2.3 重载=

    2.2.4 重载*(返回结点数据)

    2.2.5 迭代器总体代码:

    2.3 设计MyList

    2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator

    2.3.2  构造函数初始化

    2.3.3  设计begin()/end()

    2.3.4 设计插入元素 insert()

    2.3.5 设计删除 erase()

    2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()

    2.3.7 MyList整体代码

    2.4 测试案例


    1.Vector容器仿真

    1.1 定义内嵌类型表

    1. public:
    2. //内嵌类型表
    3. typedef T value;
    4. typedef T* vIter;

    1.2 定义相关属性

    1. protected:
    2. vIter m_Data;//数组头指针
    3. int m_nLen;//数组长度
    4. vIter start;//数组的起始地址
    5. int finish;//数组的满位标志
    6. int end_of_element;//数组的末尾标志

    1.3 push_back()

    1. void push_back(const value& x) {
    2. if (end_of_element != finish) {
    3. *(start + finish) = x;
    4. ++finish;
    5. }
    6. else {
    7. cout << "越界" << endl;
    8. }
    9. }

    1.4 back() 

    1. inline value back() {
    2. --finish;
    3. return *(start + finish);
    4. }

    1.5 下标操作[]

    1. value operator[](int n) {
    2. if (n <= finish) {
    3. return *(start + n);
    4. }
    5. else {
    6. cout << "取值错误<" << endl;
    7. }
    8. }

    1.6 总体代码与测试案例

    1. #include
    2. using namespace std;
    3. //MyVector的类模板
    4. template<typename T>
    5. class MyVector {
    6. public:
    7. //内嵌类型表
    8. typedef T value;
    9. typedef T* vIter;
    10. public:
    11. MyVector(int nLen = 0) :m_nLen(nLen), m_Data(nullptr), finish(0) {
    12. if (nLen > 0) {
    13. m_Data = new T[nLen];
    14. start = m_Data;
    15. end_of_element = nLen;
    16. }
    17. }
    18. ~MyVector()
    19. {
    20. }
    21. void push_back(const value& x) {
    22. if (end_of_element != finish) {
    23. *(start + finish) = x;
    24. ++finish;
    25. }
    26. else {
    27. cout << "越界" << endl;
    28. }
    29. }
    30. inline value back() {
    31. --finish;
    32. return *(start + finish);
    33. }
    34. value operator[](int n) {
    35. if (n <= finish) {
    36. return *(start + n);
    37. }
    38. else {
    39. cout << "取值错误<" << endl;
    40. }
    41. }
    42. protected:
    43. vIter m_Data;//数组头指针
    44. int m_nLen;//数组长度
    45. vIter start;//数组的起始地址
    46. int finish;//数组的满位标志
    47. int end_of_element;//数组的末尾标志
    48. };
    49. int main() {
    50. int x;
    51. MyVector<int>v(10);
    52. v.push_back(100);
    53. v.push_back(200);
    54. v.push_back(300);
    55. x = v.back();
    56. cout << "x= " << x << endl;
    57. cout << v[0] << endl;
    58. cout << v[1] << endl;
    59. cout << v[2] << endl;
    60. //cout << v[3] << endl;
    61. return 0;
    62. }

    2.List仿真

    2.1 定义结点

    1. template<typename T>
    2. struct MyList_node {
    3. MyList_node* prev;
    4. MyList_node* next;
    5. T data;
    6. };

    2.2 设计迭代器(将类型表与迭代器整合)

    1. //同时将设计类型表和迭代器整合在一起
    2. //迭代器
    3. template<typename T>
    4. struct MyList_iterator {
    5. //类型表
    6. typedef MyList_iterator iterator;
    7. typedef MyList_node* link_type;
    8. //结点指针
    9. link_type node;
    10. //构造函数
    11. MyList_iterator(link_type x) :node(x) {
    12. }
    13. MyList_iterator() :node(nullptr) {
    14. }

    2.2.1 重载++

    1. //重载++,使得我们可以获得下个结点的地址
    2. iterator& operator++() {
    3. node = node->next;
    4. //返回本对象,同时指向下一个
    5. return *this;
    6. }
    7. iterator& operator++(int) {
    8. //保存自增运算前的指针值
    9. iterator temp = *this;
    10. //本对象加一
    11. ++* this;
    12. return temp;
    13. }

    2.2.2 重载--

    1. //重载--
    2. iterator& operator--() {
    3. node = (node)->prev;
    4. return *this;
    5. }
    6. iterator& operator--(int) {
    7. iterator temp = *this;
    8. --* this;
    9. return temp;
    10. }

    2.2.3 重载=

    1. iterator& operator=(iterator x) {
    2. node = x.node;
    3. return *this;
    4. }

    2.2.4 重载*(返回结点数据)

    1. T& operator*()const {
    2. //返回结点数据
    3. return node->data;
    4. }

    2.2.5 迭代器总体代码:

    1. //迭代器
    2. template<typename T>
    3. struct MyList_iterator {
    4. //类型表
    5. typedef MyList_iterator iterator;
    6. typedef MyList_node* link_type;
    7. //结点指针
    8. link_type node;
    9. //构造函数
    10. MyList_iterator(link_type x) :node(x) {
    11. }
    12. MyList_iterator() :node(nullptr) {
    13. }
    14. //重载++,使得我们可以获得下个结点的地址
    15. iterator& operator++() {
    16. node = node->next;
    17. //返回本对象,同时指向下一个
    18. return *this;
    19. }
    20. iterator& operator++(int) {
    21. //保存自增运算前的指针值
    22. iterator temp = *this;
    23. //本对象加一
    24. ++* this;
    25. return temp;
    26. }
    27. //重载--
    28. iterator& operator--() {
    29. node = (node)->prev;
    30. return *this;
    31. }
    32. iterator& operator--(int) {
    33. iterator temp = *this;
    34. --* this;
    35. return temp;
    36. }
    37. iterator& operator=(iterator x) {
    38. node = x.node;
    39. return *this;
    40. }
    41. T& operator*()const {
    42. //返回结点数据
    43. return node->data;
    44. }
    45. };

    2.3 设计MyList

    2.3.1 数据类型表:将MyList_iterator声明为内部类型Iterator

    1. //MyList
    2. template<typename T>
    3. class MyList {
    4. public:
    5. //数据类型表
    6. //将MyList_iterator声明为内部类型Iterator
    7. typedef MyList_iterator iterator;
    8. protected:
    9. MyList_node* node;
    10. size_t length;

    2.3.2  构造函数初始化

    1. MyList() :length(0) {
    2. node = new MyList_node;
    3. node->next = node;
    4. node->prev = node;
    5. }
    6. ~MyList(){
    7. }

    2.3.3  设计begin()/end()

    1. //返回链表的头函数
    2. iterator begin() {
    3. return node->next;
    4. }
    5. //返回链表尾地址
    6. iterator end() {
    7. return node;
    8. }

    2.3.4 设计插入元素 insert()

    1. iterator insert(const iterator& position, const T& x) {
    2. MyList_node* temp = new MyList_node;
    3. temp->data = x;
    4. temp->prev = position.node->prev;
    5. temp->next = position.node;
    6. position.node->prev->next = temp;
    7. position.node->prev = temp;
    8. ++length;
    9. return temp;
    10. }

    2.3.5 设计删除 erase()

    1. void erase(const iterator& position) {
    2. position.node->prev->next = position.node->next;
    3. position.node->next->prev = position.node->prev;
    4. --length;
    5. }

    2.3.6 基于insert()、erase()设计push_back()/push_front()/pop_back()/pop_front()

    1. //push_front()
    2. void push_front(const T& x) {
    3. insert(begin(), x);
    4. }
    5. //push_back()
    6. void push_back(const T& x) {
    7. insert(end(), x);
    8. }
    9. //pop_front()
    10. void pop_front() {
    11. erase(begin());
    12. }
    13. //pop_back(){
    14. void pop_back() {
    15. erase(--end());
    16. }

    2.3.7 MyList整体代码

    1. //MyList
    2. template<typename T>
    3. class MyList {
    4. public:
    5. //数据类型表
    6. //将MyList_iterator声明为内部类型Iterator
    7. typedef MyList_iterator iterator;
    8. public:
    9. MyList() :length(0) {
    10. node = new MyList_node;
    11. node->next = node;
    12. node->prev = node;
    13. }
    14. ~MyList(){
    15. }
    16. //返回链表的头函数
    17. iterator begin() {
    18. return node->next;
    19. }
    20. //返回链表尾地址
    21. iterator end() {
    22. return node;
    23. }
    24. //push_front()
    25. void push_front(const T& x) {
    26. insert(begin(), x);
    27. }
    28. //push_back()
    29. void push_back(const T& x) {
    30. insert(end(), x);
    31. }
    32. //pop_front()
    33. void pop_front() {
    34. erase(begin());
    35. }
    36. //pop_back(){
    37. void pop_back() {
    38. erase(--end());
    39. }
    40. iterator insert(const iterator& position, const T& x) {
    41. MyList_node* temp = new MyList_node;
    42. temp->data = x;
    43. temp->prev = position.node->prev;
    44. temp->next = position.node;
    45. position.node->prev->next = temp;
    46. position.node->prev = temp;
    47. ++length;
    48. return temp;
    49. }
    50. void erase(const iterator& position) {
    51. position.node->prev->next = position.node->next;
    52. position.node->next->prev = position.node->prev;
    53. --length;
    54. }
    55. protected:
    56. MyList_node* node;
    57. size_t length;
    58. };

    2.4 测试案例

    1. int main() {
    2. MyList<int>myList1;
    3. myList1.push_front(10);
    4. myList1.push_front(20);
    5. myList1.push_front(30);
    6. MyList<int>::iterator iter;
    7. iter = myList1.begin();
    8. cout << *iter << endl;
    9. cout << *++iter << endl;
    10. cout << *++iter << endl;
    11. cout << endl;
    12. myList1.push_back(100);
    13. myList1.push_back(200);
    14. myList1.push_back(300);
    15. iter = myList1.end();
    16. cout << *--iter << endl;
    17. cout << *iter-- << endl;
    18. cout << *iter << endl;
    19. cout << endl;
    20. myList1.pop_front();
    21. myList1.pop_back();
    22. cout << *myList1.begin() << endl;
    23. cout << *--myList1.end() << endl;
    24. return 0;
    25. }

     

     

  • 相关阅读:
    c语言初阶指针
    重塑DeFi:深入了解Solaris Network
    Git创建、diff代码、回退版本、撤回代码,学废了吗
    IDEA中安装Docker插件实现远程访问Docker
    PhpStorm激活
    后仿真 不收敛
    动态路由RID ospf
    情人节程序员用HTML网页表白【我永远属于你】 HTML5七夕情人节表白网页源码 HTML+CSS+JavaScript
    Apifox--比 Postman 还好用的 API 测试工具
    屎上最全vue-pdf+Springboot与aspose-words整合,开箱即用
  • 原文地址:https://blog.csdn.net/Jason6620/article/details/126242186