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

📅  最后修改于: 2021-09-08 13:33:52             🧑  作者: Mango

任务是实现一个类似于 STL 向量的自定义向量类,具有以下功能:

  • int push_back(data):将一个元素(任何数据类型)添加到数组的末尾,并返回该向量中的元素数
  • data_type pop_back():从数组末尾移除一个元素,同时返回被弹出的元素
  • int size() const:返回向量的当前大小

下面的程序在 C++ 中实现了一个具有上述功能的自定义向量类:

CPP14
// 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 greater 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++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程