国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

2.2 Vector<T> 動態(tài)數(shù)組(模板語法)

這篇具有很好參考價值的文章主要介紹了2.2 Vector<T> 動態(tài)數(shù)組(模板語法)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

C++數(shù)據結構與算法 目錄

本文前驅課程

1 C++自學精簡教程 目錄(必讀)

2 動態(tài)數(shù)組 Vector(難度1)

其中,2 是 1 中的一個作業(yè)。2 中詳細講解了動態(tài)數(shù)組實現(xiàn)的基本原理。

本文目標

1 學會寫基本的C++類模板語法;

2 為以后熟練使用 STL 打下基礎;

3 為更進一步的閱讀和掌握更多的C++庫打下基礎;

模板語法的學習最恰當?shù)姆绞骄褪呛头悄0宕a對比學習。

本文的內容只是在 2 動態(tài)數(shù)組 Vector(難度1)的基礎上,把代碼改造為模板語法。

除此之外,不添加任何內容。

模板語法介紹

當類的成員變量是不確定的類型的時候,我們使用模板類( template class)來實現(xiàn)這樣的類。

模板類的定義如下:

    template<typename T>  
    struct Vector{
        T data;//成員變量 data 的類型不確定,寫成 T
    };

解釋說明:

(1)template 表示后續(xù)代碼中有類型是不確定的,先用typename T當中的T表示類型;

(2)typename表示T是一個未來才能確定的類型

(3)確定T的類型的方式就是創(chuàng)建一個Vector對象的時候在<>中指定:文章來源地址http://www.zghlxwxcb.cn/news/detail-690701.html

 Vector<int> arr;  //這條語句表示用int替換代碼中的T

非模板語法與模板語法差異對比圖

2.2 Vector<T> 動態(tài)數(shù)組(模板語法),C++數(shù)據結構與算法實現(xiàn),c++,開發(fā)語言,算法
模板語法和非模板語法對比1
2.2 Vector<T> 動態(tài)數(shù)組(模板語法),C++數(shù)據結構與算法實現(xiàn),c++,開發(fā)語言,算法
模板語法和非模板語法對比2

代碼與注釋如下

#include<iostream>
#include <iomanip>
#include <cassert>
using namespace std;

//------下面的代碼是用來測試你的代碼有沒有問題的輔助代碼,你無需關注------
#include <algorithm>
#include <cstdlib>
#include <iostream> 
#include <vector>
#include <utility>
using namespace std;
struct Record { Record(void* ptr1, size_t count1, const char* location1, int line1, bool is) :ptr(ptr1), count(count1), line(line1), is_array(is) { int i = 0; while ((location[i] = location1[i]) && i < 100) { ++i; } }void* ptr; size_t count; char location[100] = { 0 }; int line; bool is_array = false; bool not_use_right_delete = false; }; bool operator==(const Record& lhs, const Record& rhs) { return lhs.ptr == rhs.ptr; }std::vector<Record> myAllocStatistic; void* newFunctionImpl(std::size_t sz, char const* file, int line, bool is) { void* ptr = std::malloc(sz); myAllocStatistic.push_back({ ptr,sz, file, line , is }); return ptr; }void* operator new(std::size_t sz, char const* file, int line) { return newFunctionImpl(sz, file, line, false); }void* operator new [](std::size_t sz, char const* file, int line)
{ return newFunctionImpl(sz, file, line, true); }void operator delete(void* ptr) noexcept { Record item{ ptr, 0, "", 0, false }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); } }void operator delete[](void* ptr) noexcept {Record item{ ptr, 0, "", 0, true }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (!itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); }}
#define new new(__FILE__, __LINE__)
struct MyStruct { void ReportMemoryLeak() { std::cout << "Memory leak report: " << std::endl; bool leak = false; for (auto& i : myAllocStatistic) { if (i.count != 0) { leak = true; std::cout << "leak count " << i.count << " Byte" << ", file " << i.location << ", line " << i.line; if (i.not_use_right_delete) { cout << ", not use right delete. "; }	cout << std::endl; } }if (!leak) { cout << "No memory leak." << endl; } }~MyStruct() { ReportMemoryLeak(); } }; static MyStruct my; void check_do(bool b, int line = __LINE__) { if (b) { cout << "line:" << line << " Pass" << endl; } else { cout << "line:" << line << " Ohh! not passed!!!!!!!!!!!!!!!!!!!!!!!!!!!" << " " << endl; exit(0); } }
#define check(msg)  check_do(msg, __LINE__);
//------上面的代碼是用來測試你的代碼有沒有問題的輔助代碼,你無需關注------

//注意:禁止修改Vector的定義,包括禁止給Vector添加成員變量;
//可以添加私有成員函數(shù),如果你認為需要的話

template<typename T>
struct Vector
{
public:
    Vector();
    Vector(int n, T value);
    Vector(const Vector& from);
    Vector(int* begin, int* end);
    ~Vector();
    int size() const;
    //只讀元素
    //參考 https://zhuanlan.zhihu.com/p/539451614
    const T& operator[](int n)const { return m_data[n]; }
    //寫入元素
    T& operator[](int n) { return m_data[n]; }
    void push_back(T value);
    bool empty() const;// your job 1
    void clear();// your job 2
    Vector& operator = (const Vector& from);// your job 4
private:
    void copy(const Vector& from);// your job 3
private:
    int m_element_cout;
    int m_capacity;
    T* m_data;//定義一個元素類型待定的數(shù)組起始元素的指針
    //請忽略下面這個成員變量,這個成員變量不影響你的實現(xiàn),當它不存在即可。
};

//模板類的成員函數(shù)都要以下面的template語法開始,和類聲明的地方一樣
template<typename T>
Vector<T>::Vector()
{
    m_element_cout = 0;
    m_capacity = 10;
    m_data = new int[m_capacity];
}

template<typename T>
Vector<T>::Vector(int n, T value) :Vector()
{
    for (int i = 0; i < n; i++)
        push_back(value);
}

template<typename T>
Vector<T>::Vector(const Vector& from)
{
    m_element_cout = from.m_element_cout;
    m_capacity = from.m_capacity;
    m_data = new int[m_capacity];
    for (int i = 0; i < m_element_cout; i++)
    {
        m_data[i] = from.m_data[i];
    }
}

template<typename T>
Vector<T>::Vector(int* begin, int* end) :Vector()
{
    for (int* p = begin; p < end; p++)
    {
        push_back(*p);
    }
}

template<typename T>
Vector<T>::~Vector()
{
    delete[] m_data;
}

template<typename T>
int Vector<T>::size() const
{
    return m_element_cout;
}

template<typename T>
void Vector<T>::push_back(T value)
{
    if (m_element_cout < m_capacity)
    {
        m_data[m_element_cout] = value;
        m_element_cout++;
    }
    else
    {
        int* p;
        p = new T[2 * m_capacity];
        for (int j = 0; j < m_element_cout; j++)
        {
            p[j] = m_data[j];
        }
        p[m_element_cout] = value;
        m_element_cout = m_element_cout + 1;
        m_capacity = 2 * m_capacity;
        delete[]m_data;
        m_data = p;
    }
}

//(1) your code for : Vector empty()






//(2) your code for : Vector clear()







//(3) your code for : Vector copy()







//(4) your code for : Vector operator =









void test1(void)
{
    Vector<int> v;//創(chuàng)建一個存放int變量的容器v
    int i;
    check(v.size() == 0);
    for (i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    for (int i = 0; i < 10; i++)
    {
        check(v[i] == i);
    }
    check(v.size() == 10);
}
void test2(void)
{
    int n = 100000;
    Vector<int> v;
    int i;
    check(v.size() == 0);
    for (i = 0; i < n; i++)
    {
        v.push_back(i);
    }
    for (int i = 0; i < n; i++)
    {
        assert(v[i] == i);
    }
    check(v.size() == n);
}
void print(Vector<int>& v, const std::string& msg)
{
    std::cout << "The contents of " << msg.c_str() << " are:";
    for (int i = 0; i != v.size(); ++i)
    {
        std::cout << ' ' << v[i];
    }
    std::cout << '\n';
}
void test3()
{
    Vector<int> a;

    Vector<int> first;                   // empty vector of ints
    check(first.empty() == true && first.size() == 0);
    Vector<int> second(4, 100);                       // four ints with value 100
    check(second.empty() == false);
    check(second.size() == 4);
    Vector<int> fourth(second);                       // a copy of third
    check(fourth.size() == second.size());

    int myints[] = { 16,2,77,29 };
    Vector<int> fifth(myints, myints + sizeof(myints) / sizeof(int));
    check(fifth.empty() == false);
    check(fifth[0] == 16);
    check(fifth[3] == 29);
    check(fifth.size() == sizeof(myints) / sizeof(int));
    print(fifth, "fifth");//The contents of fifth are:16 2 77 29 
    fifth.push_back(30);
    check(fifth[4] == 30);
    check(fifth.size() == 5);
    print(fifth, "fifth");//The contents of fifth are:16 2 77 29 30 
    check(fifth.size() == sizeof(myints) / sizeof(int) + 1);
    first = fifth = fifth;
    print(first, "first");//The contents of first are:16 2 77 29 30 
    check(first.empty() == false && first.size() == fifth.size());
    Vector<int> a1(myints, myints + sizeof(myints) / sizeof(int));
    //下面大括號是作用域,用來把代碼分開互不干擾,這樣就可以創(chuàng)建相同的變量名字了,就不用為了起很多不同的變量名而煩惱了。
    {
        Vector<int> b(a1);
        b.push_back(2);
        check(b[4] == 2);
    }
    {
        Vector<int> c;
        c = a1;
    }
    check(a1.size() == sizeof(myints) / sizeof(int));
    {
        Vector<int> c;
        c = fifth;
        c[0] = 1;
        check(c[0] == 1);
    }
}

int main()
{
    test1();
    test2();
    test3();

    return 0;
}

預期輸出:

到了這里,關于2.2 Vector<T> 動態(tài)數(shù)組(模板語法)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包