Optimize memory in operators overload and template structures

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)

New contributor

user26492712 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật