关于在数组的指定位置插入值的问题 [英] Question about inserting value at specified location in an array

查看:67
本文介绍了关于在数组的指定位置插入值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在具有N个元素的数组ABC中的位置Pos处插入值M的算法


1.以M为单位的输入值.
2.在Pos中输入位置
[将元素向前移动一步]
3.在N&== POS
时重复步骤4至5
4. ABC [N + 1] = ABC [N]

5.N = N-1
[STEP-3 LOOP结束]

[在位置Pos上插入值M]

6.ABC [POS] = M
7.退出


我只是想问一下,如果我的数组大小是ABC [5],索引从0到n-1,我想在索引4插入值,那么该算法如何工作会发生什么?

如果我在索引3处插入值,那么此算法如何工作?

是数组自动增加的大小.

Algorithm to insert a value M at location Pos in an array ABC having N elements


1. input value in M.
2. Input position in Pos
[move elements one step forward]
3.Repeat step-4to 5 while N>=POS

4. ABC[N+1]=ABC[N]

5.N=N-1
[END OF STEP-3 LOOP]

[insert value M at position Pos]

6.ABC[POS]=M
7.EXIT


I simply want to ask what happen if my array size is ABC[5]and index is from 0 to n-1 and i want to insert value at index 4 then what happen how this algorithm work?

and if i insert value at index 3 then how this algorithm work?

is the size of array incearse automatically

推荐答案

这取决于数组类型.如果这是一个简单的C型数组,则您的代码将失败.对于动态扩展,您应该使用STL 数组 [
It depends on the array type. If this is a simple C-type array then your code will fail. For dynamic expansion you should use the STL array[^] class.


如果我的数组大小是ABC [4],我想在索引4处插入值
您的程序可能会崩溃,因为索引4刚好超出数组的末尾.

最好的问候
Espen Harlinn
if my array size is ABC[4] and i want to insert value at index 4
Your program might crash as index 4 is just beyond the end of the array.

Best regards
Espen Harlinn


仅当为数组分配的内存大于其当前(逻辑)大小时,提供的算法(在静态分配的数组上)有效. br/> 例如
The provided algorithm works (on a statically allocated array) only if the allocated memory for the array is bigger than its current (logical) size.
e.g.
int a[20];
int size = 0;
// fill the array
a[0] = 5; size++;
a[1] = 7; size++;
a[2] = 2; size++;
// now (logical) size = 3, while memory is allocated for 20 positions,
// that is you have 17 'spare' positions.

intert(1,4); size++; // insert 4 at position 1 (that is correct).
                     // note the logical size must be increased.



如果您动态分配数组(例如,使用
malloc [重新分配 [ std :: vector [^ ] )为您自动处理内存分配.例如:



If you allocate dynamically the array (using, for instance, malloc[^] function) then you may use, when needed, the realloc[^] function to increase the buffer.

Please note:

  • The above argument is independent on the insertion position (provided you insert within the boundaries or append the item). It depends only on logical size vs physical allocated memory.
  • C++ provides containers classes (like for instance std::vector[^]) that handle automatically memory allocation for you. For instance:

    #include <vector>
    #include <iostream>
    using namespace std;
    int main()
    {
      vector <int> v;
      v.push_back(5); 
      v.push_back(7);
      v.push_back(2);
    
      vector<int>::iterator it = v.begin() + 1;
      v.insert(it, 4); // insert the element, no need to manage array's growth
      cout << v[1] << endl;
    
    }


  • 这篇关于关于在数组的指定位置插入值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆