优化运算符重载和模板结构中的内存

问题描述投票：0回答：1

``````template<typename T>
struct FlexArray;
``````

``T``

``at(unsigned int index)``

``at(i)``

``=``

Size()方法，返回数组的当前大小。

``operator+``
``FlexArrays``

``operator+=``
，其中
``FlexArray``

``{1, 3, 5} + {2, 4} = {1, 3, 5, 2, 4}``

``FlexArrays``

``operator +=``

``FlexArray``

``operator*``

``operator*=``

``FlexArray``
``k > 0``
，其中乘以数字
``k``

``k``

``{1, 2} * 3 = {1, 2, 1, 2, 1, 2}``
。我们可以假设
``k``
``unsigned long``

``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

}
``````

``````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>&);
``````

``````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;
}
``````

c++ c++17 g++
1个回答
0

``push_back()``

``vector::insert()``

``````template<typename T>
FlexArray<T>& FlexArray<T>::operator += (const FlexArray& another) {
vec.insert(vec.end(), another.vec.begin(), another.vec.end());
return *this;
}
``````

``vector::reserve()``

``````template<typename T>
FlexArray<T>& FlexArray<T>::operator *= (size_t k) {
if (k > 1) {
auto copy = *this;
vec.reserve(vec.size() * k);
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) {
FlexArray<T> copy;
copy.vec.reserve(left.Size() + right.Size());
copy += left;
copy += right;
return copy;
}
``````

``vector``

``````template<typename T>
FlexArray<T>& FlexArray<T>::operator += (const FlexArray& another) {
size_t oldSize = vec.size();
vec.resize(oldSize + another.Size());
std::copy(another.vec.begin(), another.vec.end(), vec.begin()+oldSize);
return *this;
}

template<typename T>
FlexArray<T>& FlexArray<T>::operator *= (size_t k) {
if (k > 1) {
size_t oldSize = vec.size();
vec.resize(oldSize * k);
for(size_t i = 0; i < oldSize; ++i) {
for (size_t j = 1; j < k; ++j) {
vec[i+(oldSize*j)] = vec[i];
}
}
}
else if (k == 0) {
vec.clear();
}
return *this;
}

template<typename T>
FlexArray<T> operator+(const FlexArray<T>& left, const FlexArray<T>& right) {
FlexArray<T> copy(left.Size() + right.Size());
std::copy(left.vec.begin(), lefft.vec.end(), copy.vec.begin());
std::copy(right.vec.begin(), right.vec.end(), copy.vec.begin()+left.Size());
return copy;
}

template<typename T>
FlexArray<T> operator*(const FlexArray<T>& arr, size_t k) {
FlexArray<T> copy(arr.Size()*k);
auto iter = copy.vec.begin();
for(size_t i = 0; i < k; ++i) {
std::copy(arr.vec.begin(), arr.vec.end(), iter);
iter += arr.Size();
}
return copy;
}
``````