c++ vector

Please help me to perform this code! I'm beginner and want to know how it's look like!
thank you


To create abstract data type (structure) - vector that comprises a pointer to the target and the number of elements. To define initialization functions, the removal of a vector, set / change the size of the vector, to access to the elements of the vector, a vector calculation module. In the main function to perform addition of two vectors.
Last edited on
If you mean C++ vector(in the STL) you could check this link for info on how your functons should be like:
http://www.cplusplus.com/reference/vector/vector/

Aceix.
the removal of a vector

What does this mean?

Code below is C++98 and was not tested.

It should be a decent beginning, and all you need to do is overload:
1) operator+ to "perform addition of two vectors"
2) operator[] to access elements individually
3) operator<< to print the Vector to std::ostream

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
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <ostream>

template <typename ElementType>
class Vector
{
private:

    ElementType *target;
    std::size_t number_of_elements;

public:

    Vector():
        target(NULL),
        number_of_elements(0)
    {
    }

    Vector(const Vector &v):
        target(new ElementType[v.number_of_elements]),
        number_of_elements(v.number_of_elements)
    {
        std::copy_n(v.target, v.number_of_elements, target);
    }

    Vector & operator = (const Vector &v)
    {
        if (this != &v)
        {
            delete[] target;
            number_of_elements = v.number_of_elements;
            target = new ElementType[v.number_of_elements];
            std::copy_n(v.target, v.number_of_elements, target);
        }

        return *this;
    }

    ~Vector()
    {
        delete[] target;
    }

    std::size_t size() const
    {
        return number_of_elements;
    }

    // warning, this can be destructive
    void resize(std::size_t n)
    {
        number_of_elements = n;

        if (n == 0)
        {
            delete[] target;
            target = NULL;
        }
        else
        {
            ElementType *tmp = new ElementType[n];

            std::copy_n(target, n, tmp);
            delete[] target;
            target = tmp;
        }
    }

    ElementType & operator [] (std::size_t i)
    {
        // your code here
    }

    const ElementType & operator [] (std::size_t i) const
    {
        // your code here
    }

    friend Vector operator + (const Vector &, const Vector &);
    friend std::ostream & operator << (std::ostream &, const Vector &);
};

Vector operator + (const Vector &v1, const Vector &v2)
{
    // your code here
}

std::ostream & operator << (std::ostream &os, const Vector &v)
{
    // your code here
}

int main()
{
    Vector<int> v1, v2;

    v1.resize(3);
    v2.resize(1);

    v1[0] = 3;
    v1[1] = 2;
    v1[2] = 1;
    v2[0] = 0;

    std::cout << v1 + v2 << std::endl; // should print `3 2 1 0'
}

Topic archived. No new replies allowed.