通过使用数组和数据结构,可以在多个内存位置上存储同质(相同)数据。使用数组的关键好处是我们可以使用索引参数从任何位置检索它们。这种数据结构变得线性,因为数据必须逐步插入和提取。我们只需要将该元素的索引或位置号放在方括号内,就可以从数组中检索它。在本文中,我们将使用数组A和另一个元素e。我们将在C++中将e插入到A的起始位置。
理解概念并举例说明
'Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
After inserting 23 at the end, the array will look like this:
[23, 10, 14, 65, 85, 96, 12, 35, 74, 69]
在上面的示例中,我们有一个包含九个元素的数组A。我们将在数组A的开头插入另一个元素23。结果数组包含了所有元素以及开头的23。要在开头插入一个元素,我们必须将所有元素向右移动一个位置,然后第一个槽位将为空,我们将新元素放在该位置。让我们看一下算法,以便更清楚地理解。
算法
取一个数组 A 和一个元素 e
如果数组 A 有足够的空间来插入元素 e,则
对于从n-1到0的范围内的i,执行以下操作:
A[ i + 1 ] = A[ i ]
结束循环
A[0]=e
增加 n 1
结束如果
返回 A
Example
的中文翻译为:示例
'#include <iostream>
# define Z 50
using namespace std;
void displayArr(int arr[], int n){
for( int i = 0; i < n; i++ ){
cout << arr[ i ] << ", ";
}
cout << endl;
}
void insertAtBeginning( int A[], int &n, int e ){
if( n + 1 < Z ) {
for( int i = n - 1; i >= 0; i-- ) {
A[ i + 1 ] = A[ i ];
}
A[ 0 ] = e;
n = n + 1;
}
}
int main() {
int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
int n = 12;
cout << "Array before insertion: ";
displayArr( A, n );
cout << "Inserting 58 at the beginning:" << endl;
insertAtBeginning( A, n, 58 );
cout << "Array after insertion: ";
displayArr( A, n );
cout << "Inserting 225 at the beginning:" << endl;
insertAtBeginning( A, n, 225 );
cout << "Array after insertion: ";
displayArr( A, n );
}
输出
'Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
使用向量在前面插入一个元素
向量是一种动态数据结构,它是C++ STL的一部分。我们可以在向量中获得与数组类似的功能。但是在向量中,我们只能在末尾或后面插入。没有直接的方法可以在开头插入。然而,我们可以像之前一样将元素向后移动一个位置,然后在开头插入新元素。或者我们可以创建另一个只包含新元素的单元素向量,然后将它们连接起来。因此,结果向量将包含所有先前的元素和新元素在开头。让我们看一下算法和C++实现。
算法
取一个数组 A 和一个元素 e
创建一个空向量 B
将 e 插入到 B 中
A := 将 B 与 A 连接起来(先 B 再 A)
返回 A
Example
的中文翻译为:示例
'#include <iostream>
#include <vector>
# define Z 50
using namespace std;
void displayArr( vector<int> v ){
for( int i = 0; i < v.size() ; i++ ){
cout << v[ i ] << ", ";
}
cout << endl;
}
vector<int> insertAtBeginning( vector<int> A, int e ){
vector<int> B( 1 );
B[ 0 ] = e;
B.insert( B.end(), A.begin(), A.end() );
return B;
}
int main() {
vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
cout << "Array before insertion: ";
displayArr( A );
cout << "Inserting 58 at the beginning:" << endl;
A = insertAtBeginning( A, 58 );
cout << "Array after insertion: ";
displayArr( A );
cout << "Inserting 225 at the beginning:" << endl;
A = insertAtBeginning( A, 225 );
cout << "Array after insertion: ";
displayArr( A );
}
输出
'Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Inserting 58 at the beginning:
Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Inserting 225 at the beginning:
Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
结论
在本文中,我们已经看到了如何在数组的开头插入元素。这里我们讨论了两种不同的解决方案。第一种解决方案使用了静态的C++数组,而第二种解决方案使用了向量。向量没有直接在开头插入元素的方法。我们可以直接在末尾插入元素使用push_back()方法。为此,我们使用了一个技巧,创建一个大小为1的数组,然后将新元素插入其中。然后将其与给定的数组连接起来。我们可以使用列表来实现相同的效果。然而,C++列表有push_front()方法,可以直接在前面插入元素。但是列表不是数组,它们是链表。