[英]How do I reverse a dynamic array in C++?
This is my first attempt of reversing a dynamic array: 这是我反转动态数组的首次尝试:
bool reverse()
{
T *newArray = NULL;
// Validate operation.
if (!isValid() || isReadOnly())
return false;
// Allocate new array
newArray = new (std::nothrow)T[m_size];
if (newArray == NULL)
return false;
// Reverse the array's contents.
for (int i = m_size - 1; i >= 0; i--)
newArray[i] = m_array[i];
// Delete old array.
delete[] m_array;
m_array = NULL;
// Assign new array
m_array = newArray;
return true;
}
As you can imagine, this is very costly for large arrays: 您可以想象,这对于大型阵列而言非常昂贵:
I'm aware of std::reverse, but unfortunately it doesn't work on dynamic arrays. 我知道std :: reverse,但不幸的是它不适用于动态数组。
Should I use std::vector? 我应该使用std :: vector吗? Yes.
是。 But this is for learning.
但这是为了学习。 I'm reading from a data structures game programming book and extending my learning.
我正在阅读一本数据结构游戏编程书,并扩展了我的学习范围。
So I'm interested in reducing this member function of Array to the algorithm itself: 因此,我有兴趣将Array的该成员函数简化为算法本身:
// Reverse the array's contents.
for (int i = m_size - 1; i >= 0; i--)
newArray[i] = m_array[i];
I feel like there's an easy way to go about this that is much less costly. 我觉得有一个简单的方法可以降低成本。 I looked on Google but I'm just finding solutions for static arrays.
我在Google上看过,但是我只是在寻找静态数组的解决方案。
Thanks in advanced. 提前致谢。
Extra: 额外:
I'm trying std::reverse again, but no luck so far. 我正在尝试再次std :: reverse,但到目前为止还没有运气。
std::reverse(std::begin(m_array), std::end(m_array));
Error on compile: 编译错误:
error C2672: 'begin': no matching overloaded function found
错误C2672:“开始”:找不到匹配的重载函数
Also, std::end wouldn't know the end of a dynamic array, as no size is specified, so maybe I'm just using the wrong functions to achieve this goal. 另外,std :: end不会知道动态数组的结尾,因为没有指定大小,所以也许我只是使用错误的函数来实现此目标。 It'd be nice to use std::reverse somehow.
最好以某种方式使用std :: reverse。
std::reverse(m_array+0, m_array+m_size);
std::reverse
将迭代器作为参数,而指针是迭代器的一种形式。
It works fine as you can use pointers with every std function which can use iterators: 它可以正常工作,因为您可以在每个可以使用迭代器的std函数中使用指针:
int size = 10;
int *i = new int[size];
iota(i, i + size, 0);
copy(i, i + size, ostream_iterator<int>(cout, " "));
reverse(i, i + size);
copy(i, i + size, ostream_iterator<int>(cout, " "));
0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
You can check this article Raw pointers are also Iterators! 您可以查看本文原始指针也是迭代器! .
。
You could manually swap the starting indices with the ending indices to effectively reverse the array. 您可以手动将开始索引与结束索引交换,以有效地反转数组。
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
int* array = new int[6]{ 1, 2, 3, 4, 5, 6 };
constexpr std::size_t size = 6;
//swap ending and starting iterators
for (std::size_t index = 0, end = size / 2; index != end; ++index) {
std::swap(array[index], array[size - index - 1]);
}
for (std::size_t index = 0; index != size; ++index) {
std::cout << array[index] << ' ';
}
std::cout << std::endl << std::endl;
std::reverse(array, array + size);
for (std::size_t index = 0; index != size; ++index) {
std::cout << array[index] << ' ';
}
delete[] array;
return 0;
}
std::reverse
Will also work since it accepts a starting and ending iterator to which pointers can act as iterators. std::reverse
也将起作用,因为它接受指针可以充当迭代器的开始和结束迭代器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.