在C++数组中移动元素
Shifting elements in C++ Array
我有一个有 35 个插槽的整数数组。我想插入一个值,每次插入新值时,我希望第一个值保持尾部,新值成为头部。我不能使用链表或队列,我必须使用 void 函数。我无法弄清楚算法,但是我想到的一切都包括了一个for循环,我只是不知道如何正确实现它。
数组列表.cpp
#include <iostream>
#include "ArrayList.h"
using namespace std;
ArrayList::ArrayList() {
capacity = 8;
length = 0;
array = new int[capacity];
}
ArrayList::ArrayList(const ArrayList& other) {
length = other.length;
capacity = other.capacity;
array = new int[other.capacity];
for (int i = 0; i <= capacity; i++)
array[i] = other.array[i];
}
void ArrayList::add(int item) {
if (length <= capacity) {
changeCapacityTo(2 * capacity);
}
length++;
array[length++] = item;
}
void ArrayList::add(int index, int item) {
while (index > capacity || length == capacity) {
capacity *= 2;
}
if (length != 0 && length < index) {
length = index;
}
int temp;
int i;
for (i = 0; i <= length; i++) {
array[index] = item;
temp = array[index];
array[index + 1] = temp;
}
length++;
}
int ArrayList::get(int index) const {
return array[index];
}
void ArrayList::changeCapacityTo(int newCapacity) {
int *newArray = new int[newCapacity];
int numItemsToCopy = length < newCapacity ? length : newCapacity;
for (int i = 0; i < numItemsToCopy; i++)
newArray[i] = array[i];
delete[] array;
array = newArray;
}
testArrayList.cpp
#include <iostream>
#include "ArrayList.h"
using namespace std;
void verifyArrayList(ArrayList arrayList) {
for(int i = 0; i < 1000; ++i) {
int item;
int itemToAdd = 2 * i;
if((item = arrayList.get(i)) != itemToAdd)
cout << "OOPS - Error at index " << i << ": " << item << " should be "
<< itemToAdd << endl;
}
}
void printArrayList(ArrayList arrayList) {
for(int i = 0; i < arrayList.getLength(); ++i) {
int item = arrayList.get(i);
cout << i << ":" << item << endl;
}
}
int main(int argc, char *argv[]) {
ArrayList arrayList;
const int highIndex = 35;
for(int i = highIndex; i > 0; --i) {
int itemToAdd = 2 * i;
arrayList.add(1, itemToAdd);
//cout << "VALUE OF itemToAdd: " << itemToAdd << endl;
//cout << endl;
}
arrayList.add(99);
printArrayList(arrayList);
cout << "#items = " << arrayList.getLength() << endl;
cout << "capacity = " << arrayList.getCapacity() << endl;
arrayList.add(2000, 9999);
cout << "#items = " << arrayList.getLength() << endl;
cout << "capacity = " << arrayList.getCapacity() << endl;
}
ArrayList.h
#ifndef ARRAYLIST_H_
#define ARRAYLIST_H_
#include <iostream>
class ArrayList {
public:
/*
* Initialize list with a capacity of 8
*/
ArrayList();
/*
* Copy constructor
*/
ArrayList(const ArrayList& other);
virtual ~ArrayList() {
std::cout << "Destructing ArrayList at " << array << std::endl;
delete [] array;
array = NULL;
}
/*
* Add item to end of list
* @param item item to add to list
*/
void add(int item);
/*
* Adds item to list, at index, shifting items as necessary and increasing
* capacity of list as necessary. If capacity must increase, it must always
* be a power of 2. Note that if index is beyond capacity, capacity must be
* increased to allow adding the item at that index. Also, length should
* reflect the HIGHEST index (plus one, naturally) at which an item is
* stored, even if lower-indexed slots contain undefined values.
*
* @param item item to add to list
*/
void add(int index, int item);
/*
* Return item at index. For now, we assume index is legal.
* Later we will throw an exception when index is illegal.
* @param index index of item to return
* @return item at index
*/
int get(int index) const;
/*
* Return capacity
* @return capacity
*/
int getCapacity() const {
return capacity;
}
/*
* Return current length
* @return current length
*/
int getLength() const {
return length;
}
private:
int *array;
int length;
int capacity;
/*
* Change capacity to that specified by newCapacity.
* @param newCapacity the new capacity
*/
void changeCapacityTo(int newCapacity);
};
#endif /* ARRAYLIST_H_ */
输出应如下所示:正确的输出
这是我的输出的样子:我的输出
新功能
我正在尝试反向复制array
的前 35 个值,将它们分配给reverseArray
,然后将相反的顺序分配回数组。下面的代码无法正常工作,无法达到我的预期。
void ArrayList::add(int index, int item) {
while (index > capacity || length == capacity) {
capacity *= 2;
}
if (length != 0 && length < index) {
length = index;
}
int temp;
int i;
for (i = 0; i <= length; i++) {
array[index] = item;
temp = array[index];
array[index + 1] = temp;
}
length++;
if(length > 35) {
int reverseArray[35];
reverse_copy(array, array + 35, reverseArray);
for(int i = 0; i <= 35; i++) {
array[i] = reverseArray[i];
}
}
}
- 初始化指向数组末尾的指针。
- 对于每个新元素,将其设置为当前指针指向的值,并将当前指针减少 1。
如果使用 void 返回函数,则可以传递新元素值和当前指针作为参数。并注意检查当前指针。
相关文章:
- 在同一容器中移动元素
- 从标准容器中移动元素是否合法?
- 我可以在 std::list 中移动元素而不会使迭代器或引用无效,但是如何移动呢?
- 在数组中移动元素 SIGABRT 错误
- C++11 在列表到映射(或其他容器)之间移动元素
- 在堆栈中移动元素
- 初始化 std::数组而不复制/移动元素
- 算法,用于查找获取排序列表所需的最小移动元素到结束步骤
- 从Boost Multi_index数组移动元素
- 将一个向量附加到另一个向量时,为什么移动元素比复制它们便宜?
- memmove会移动元素(就像前面的方式相同),还是一次抓住整个内存块
- 在C++数组中移动元素
- 为什么重新分配矢量复制而不是移动元素
- 在矢量中插入元素并在其后移动元素
- 弹出后使用数组>>移动元素排队
- 从STL容器中移动元素是否会将其从该容器中移除
- 对容器进行排序,然后在保留原始排序的情况下移动元素
- 可移动元素向量的大小调整是否有效?
- 我如何构建公共数据,同时保留默认的移动元素和分配
- 扩展std::vector以从其他vector类型移动元素