通过使用数组和数据结构,可以在多个内存位置上存储同质(相同)数据。使用数组的关键好处是我们可以使用索引参数从任何位置检索它们。这种数据结构变得线性,因为数据必须逐步插入和提取。我们只需要将该元素的索引或位置号放在方括号内,就可以从数组中检索它。在本文中,我们将使用数组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 50using 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 50using 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()方法,可以直接在前面插入元素。但是列表不是数组,它们是链表。
以上就是c++程序在数组开头添加元素的详细内容。