The task:
In this task you have to write a structure that will work both like a regular array in C and like a list in Python. Namely, write a structure
template
struct FlexArray;
Those. this is a template structure. It is an array of elements of type T of fixed length, and the length is passed in the constructor. By default, you should assume that the length is 0. (This does not oblige you to use only classic arrays when implementing)
Elements are accessed through the at(unsigned int index) method – returns the index element of the array. Write this method correctly for both non-const and const structures. Namely, calling at(i) on constant structures should not allow the contents of the array to be changed, and moreover, cannot appear to the left of the = sign
Size() method, which returns the current size of the array.
Now comes the fun part.
Write operator+ from two FlexArrays and operator+=, where the addition of a FlexArray is considered to be the concatenation of their elements. Those. {1, 3, 5} + {2, 4} = {1, 3, 5, 2, 4}.
You can add two different FlexArrays only if their template arguments match.
operator += should effectively add a new array to the current FlexArray.
Also write operator* and operator*= from FlexArray and number k > 0, where multiplication by number k is a concatenation of itself k times. Those. {1, 2} * 3 = {1, 2, 1, 2, 1, 2}. We can assume that k is unsigned long
You need to be able to multiply both from left to right and from right to left. Those. There are only 3 options possible FlexArray *= k, FlexArray * k, k * FlexArray
int main() {
FlexArray<int> flex(3);
flex.at(0) = 1;
flex.at(1) = 2;
flex.at(2) = 3;
std::cout << flex.Size(); // 3
auto prod = flex * 2;
for (int i = 0; i < prod.Size(); ++i) {
std::cout << prod.at(i) << " ";
}
std::cout << "n";
// Should output 1 2 3 1 2 3
FlexArray<int> last(2);
last.at(0) = 4;
last.at(1) = 5;
flex += last;
for (int i = 0; i < flex.Size(); ++i) {
std::cout << flex.at(i) << " ";
}
std::cout << "n";
// Should output 1 2 3 4 5
}
So, the implementation of these methods is up to you; the return types are not specifically written here:
FlexArray<T>::FlexArray();
FlexArray<T>::FlexArray(size_t);
FlexArray<T>::at(size_t);
FlexArray<T>::Size();
FlexArray<T>::operator += (const FlexArray&);
FlexArray<T>::operator *= (size_t);
operator+(const FlexArray<T>&, const FlexArray<T>&);
operator*(const FlexArray<T>&, size_t);
operator*(size_t, const FlexArray<T>&);
Send only the structure code to the system + possibly necessary # include
This is my solution:
template<typename T>
struct FlexArray {
std::vector<T> vec;
FlexArray();
FlexArray(size_t n);
T& at(size_t index);
const T& at(size_t index) const;
size_t Size() const;
FlexArray& operator += (const FlexArray& another);
FlexArray& operator *= (size_t k);
};
template<typename T>
FlexArray<T>::FlexArray() : vec(0){}
template<typename T>
FlexArray<T>::FlexArray(size_t n) : vec(n) {}
template<typename T>
T& FlexArray<T>::at(size_t index) {
return vec[index];
}
template<class T>
const T& FlexArray<T>::at(size_t index) const {
return vec[index];
}
template<typename T>
size_t FlexArray<T>::Size() const{
return vec.size();
}
template<typename T>
FlexArray<T>& FlexArray<T>::operator += (const FlexArray& another) {
for (size_t i = 0; i < another.Size(); i++) {
vec.push_back(another.at(i));
}
return *this;
}
template<typename T>
FlexArray<T>& FlexArray<T>::operator *= (size_t k) {
auto copy = *this;
for (size_t i = 1; i < k; i++) {
*this += copy;
}
return *this;
}
template<typename T>
FlexArray<T> operator+(const FlexArray<T>& left, const FlexArray<T>& right) {
auto copy = left;
copy += right;
return copy;
}
template<typename T>
FlexArray<T> operator*(const FlexArray<T>& arr, size_t k) {
auto copy = arr;
copy *= k;
return copy;
}
template<typename T>
FlexArray<T> operator*(size_t k, const FlexArray<T>& arr) {
return arr * k;
}
the problem is that it doesn’t pass the memory test(64 MB limit)
my code gives out 100ms / 86.77Mb
The tesing sistem uses g++17 7.3 controlled
How can i optimize that?(the task was taken from an online course and I’m sure that the code can be optimized somehow not very difficult, or I made a mistake somewhere, thanks in advance for your help)
user26492712 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.