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.

## All 3 Replies

Sounds like school work. We don't do it for you. First, make an effort to solve the assignment. Then, post your code here. We will be happy to help you understand/fix your issues/problems at that point.

``````#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)
{
}

const ElementType & operator [] (std::size_t i) const
{
}

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

Vector operator + (const Vector &v1, const Vector &v2)
{
}

std::ostream & operator << (std::ostream &os, const Vector &v)
{
}

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

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

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

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

So what exactly are you haveing a problem with? What have you tried?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.