C++重写vector

int main()

{

    Vector<int> vec;

    //添加元素

    vec.push_back(1);

    vec.push_back(2);

    vec.push_back(3);

    vec.push_back(3);

    vec.toString();

     

    //弹出最后一个元素

    vec.pop_back();

    vec.toString();

 

    //直接取某个元素

    const int a = vec[0];

    cout<<"vec[1]:"<<vec[1]<<endl;

 

 

 

    return 0;

}

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

#include "stdafx.h"

#include <iostream>

using namespace std;

 

//template 关键字后接模板形参表,表明这里定义的Vector是一个类模板,而不是一个类,

//Vector<int>才是一个类.函数模板也是一样,它们都只是一个"公式".

template <typename Object>

class Vector

{

public:

    static const int SPARE_CAPACITY = 16;

     

    //将构造函数声明为explicit ,是为了抑制由构造函数定义的隐式转换

    /* 

        构造函数的初始化列表.关于构造函数的初始化列表, 有两个要点.第一, 即使列表为空, 没有初始化式,

        构造函数也会先初始化每个成员再进入函数体, 这时初始化的规则与初始化变量相同,

        由此得知第二个要点:如果类的成员本身是一个没有默认构造函数的类类型, 或者成员是const、引用类型,

        这样的成员必须在构造函数初始化列表中进行初始化

    */

    explicit Vector(int initSize = 0) :theSize(initSize), theCapacity(initSize + SPARE_CAPACITY)

    {

        //new返回一个指向Object类型数组的指针

        objects = new Object[theCapacity];

    }

    Vector(const Vector& rhs) :objects(NULL)

    {

        operator=(rhs);

    }

    ~Vector()

    {

        delete[] objects;

    }

 

    const Vector& operator=(const Vector& rhs)

    {

        if (this != &rhs)

        {

            delete[] objects;

            theSize = rhs.theSize;

            theCapacity = rhs.theCapacity;

 

            objects = new Object[theCapacity];

            for (int k = 0; k < theSize; k++)

            {

                objects[k] = rhs.objects[k];

            }

        }

        return *this;

    }

 

    /*

        由于成员函数的定常性是(即函数名后是否有const关键字)是签名的一部分,

        因此我们可以使用访问函数的operator[]版本返回const引用,而修改函数版本返回一般引用

    */

    Object& operator[](int index)

    {

        if (index < 0 || index >= theSize)

        {

            return objects[0];

        }

        cout << "--Object& operator[](int index)"<<endl;

        return objects[index];

    }

    const Object& operator[](int index)const

    {

        cout << "** const Object& operator[](int index)const" << endl;

        return objects[index];

    }

 

 

    //检测是否需要扩容

    void reserve()

    {

        reserve(theSize);

    }

    void reserve(int newSize)

    {

        if (theCapacity > newSize)

        {

            return;

        }

        int newCapacity = theCapacity * 2 + 1;

        Object* oldArr = objects;

        objects = new Object[newCapacity];

        for (int k = 0; k < theSize; k++)

        {

            objects[k] = oldArr[k];

        }

        theCapacity = newCapacity;

        delete[] oldArr;

    }

 

    int size()const

    {

        return theSize;

    }

    int capacity()const

    {

        return theCapacity;

    }

    bool empty()const

    {

        return theSize == 0;

    }

    void resize(int newSize)

    {

        reserve(newSize);

        theSize = newSize;

        theCapacity = newSize;

    }

 

    void push_back(const Object& obj)

    {

        reserve();      //检测容器大小

        objects[theSize++] = obj;

    }

 

    void pop_back()

    {

        theSize--;

    }

    const Object & back()const

    {

        return objects[theSize - 1];

    }

 

    Object* begin()

    {

        return &objects[0];

    }

    Object* end()

    {

        return &objects[theSize];

    }

     

    const Object* end()const

    {

        return&objects[theSize];

    }

 

    void toString()

    {

        cout << "Vecot长度:" << size() << ",容量:" << capacity() << endl;

        for (int i = 0; i < theSize; i++)

        {

            cout << "objects[" << i << "]:" << objects[i] << endl;

        }

    }

 

    typedef Object* iterator;

    typedef const Object* const_iterator;

private:

    int theSize;

    int theCapacity;

    Object* objects;

};

  

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淮城一只猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值