首页 > 后端开发 > C++ > 正文

C++程序在数组开头添加元素

WBOY
发布: 2023-09-08 15:01:02
转载
1787人浏览过

c++程序在数组开头添加元素

通过使用数组和数据结构,可以在多个内存位置上存储同质(相同)数据。使用数组的关键好处是我们可以使用索引参数从任何位置检索它们。这种数据结构变得线性,因为数据必须逐步插入和提取。我们只需要将该元素的索引或位置号放在方括号内,就可以从数组中检索它。在本文中,我们将使用数组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,则

    立即学习C++免费学习笔记(深入)”;

    • 对于从n-1到0的范围内的i,执行以下操作:

      • A[ i + 1 ] = A[ i ]

    • 结束循环

    • A[0]=e

    • 增加 n 1

  • 结束如果

    序列猴子开放平台
    序列猴子开放平台

    具有长序列、多模态、单模型、大数据等特点的超大规模语言模型

    序列猴子开放平台0
    查看详情 序列猴子开放平台
  • 返回 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()方法,可以直接在前面插入元素。但是列表不是数组,它们是链表。

以上就是C++程序在数组开头添加元素的详细内容,更多请关注php中文网其它相关文章!

相关标签:
c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号