📜  在C ++中创建自定义矢量类的程序

📅  最后修改于: 2021-06-29 19:32:44             🧑  作者: Mango

任务是使用以下功能实现类似于STL向量的自定义向量类:

  • int push_back(data):在数组的末尾添加一个元素(任何data_type),并返回该向量中的元素数
  • data_type pop_back():从数组末尾删除元素,还返回弹出的元素
  • int size()const:返回向量的当前大小

下面的程序使用上述功能在C++中实现一个自定义矢量类:

// C++ program to implement Custom Vector
// class
#include "iostream"
#define ll long long
using namespace std;
  
// Template class to create vector of
// different data_type
template 
class GenericMyVector {
private:
    DT* arr;
  
    // Variable to store the current capacity
    // of the vector
    ll capacity;
  
    // Variable to store the length of the
    // vector
    ll length;
  
public:
    explicit GenericMyVector(ll = 100);
  
    // Function that returns the number of
    // elements in array after pushing the data
    ll push_back(DT);
  
    // function that returns the popped element
    DT pop_back();
  
    // Function that return the size of vector
    ll size() const;
    DT& operator[](ll);
  
    // Iterator Class
    class iterator {
    private:
        // Dynamic array using pointers
        DT* ptr;
  
    public:
        explicit iterator()
            : ptr(nullptr)
        {
        }
        explicit iterator(DT* p)
            : ptr(p)
        {
        }
        bool operator==(const iterator& rhs) const
        {
            return ptr == rhs.ptr;
        }
        bool operator!=(const iterator& rhs) const
        {
            return !(*this == rhs);
        }
        DT operator*() const
        {
            return *ptr;
        }
        iterator& operator++()
        {
            ++ptr;
            return *this;
        }
        iterator operator++(int)
        {
            iterator temp(*this);
            ++*this;
            return temp;
        }
    };
  
    // Begin iterator
    iterator begin() const;
  
    // End iterator
    iterator end() const;
};
  
// Template class to return the size of
// vector of different data_type
template 
GenericMyVector
::GenericMyVector(ll n)     : capacity(n), arr(new DT[n]), length(0) { }    // Template class to insert the element // in vector template ll GenericMyVector
::push_back(DT data) {     if (length == capacity) {         DT* old = arr;         arr = new DT[capacity = capacity * 2];         copy(old, old + length, arr);         delete[] old;     }     arr[length++] = data;     return length; }    // Template class to return the popped element // in vector template DT GenericMyVector
::pop_back() {     return arr[length-- - 1]; }    // Template class to return the size of // vector template ll GenericMyVector
::size() const {     return length; }    // Template class to return the element of // vector at given index template DT& GenericMyVector
::operator[](ll index) {     // if given index is greator than the     // size of vector print Error     if (index >= length) {         cout << "Error: Array index out of bound";         exit(0);     }        // else return value at that index     return *(arr + index); }    // Template class to return begin iterator template typename GenericMyVector
::iterator                        GenericMyVector
::begin() const {     return iterator(arr); }    // Template class to return end iterator template typename GenericMyVector
::iterator                          GenericMyVector
::end() const {     return iterator(arr + length); }    // Template class to display element in // vector template void display_vector(V& v) {     // Declare iterator     typename V::iterator ptr;     for (ptr = v.begin(); ptr != v.end(); ptr++) {         cout << *ptr << ' ';     }     cout << '\n'; }    // Driver code int main() {     cout << "For Integer data_type" << endl;        GenericMyVector v;     v.push_back(5);     v.push_back(6);     v.push_back(7);     v.push_back(8);     v.push_back(9);     v.push_back(10);     v.push_back(11);     v.push_back(12);        // Declare iterator for traversal in     // vector v     GenericMyVector::iterator it;        // Function called to display element in     // vector in v     cout << "Element in vector v : ";     display_vector(v);        // Print the size of vector v     cout << "size: " << v.size() << endl;        // Print Element at index 2     cout << "v[2]: " << v[2] << '\n';        // Pop an element and print it     cout << "Popped Element: " << v.pop_back() << '\n';     display_vector(v);        cout << endl;        cout << "For Char data_type" << endl;        GenericMyVector c;     c.push_back('a');     c.push_back('b');     c.push_back('c');     c.push_back('d');     c.push_back('e');     c.push_back('f');     c.push_back('g');     c.push_back('h');        // Declare iterator for traversal in     // vector c     GenericMyVector::iterator it1;        // Function called to display element in     // vector in c     cout << "Element in vector c : ";     display_vector(c);        // Print the size of vector c     cout << "size: " << c.size() << '\n';        // Print Element at index 2     cout << "c[2]: " << c[2] << '\n';        // Pop an element and print it     cout << "pop: " << c.pop_back() << '\n';        // Function called to display element in     // vector in c     cout << "Element in vector c : ";     display_vector(c);     return 0; }
输出:
For Integer data_type
Element in vector v : 5 6 7 8 9 10 11 12 
size: 8
v[2]: 7
Popped Element: 12
5 6 7 8 9 10 11 

For Char data_type
Element in vector c : a b c d e f g h 
size: 8
c[2]: c
pop: h
Element in vector c : a b c d e f g
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”