我想在向量中的某个位置插入多个值

发布于 2025-01-15 05:03:42 字数 716 浏览 4 评论 0原文

当我创建一个向量时,假设大小为 5 ,元素为 1,2,3,4,5 ,我想在位置(例如索引 2)添加数字 200 和 300 ,向量应该看起来像 1 , 2、200、300、3、4、5。

#include <stdio.h>
#include <stdlib.h>

int main(){
    int v[] ={1,2,3,4,5,6,7};
    int n = sizeof(v)/sizeof(int);
    int location,element;
    printf("Enter location:");
    scanf("%d",&location);
    printf("Enter element:");
    scanf("%d",&element);
    for(int i = n - 1; i >= location;i--){
        v[i + 1] = v[i];
    }
    v[location] = element;
    for(int i = 0; i <= n;i++){
        printf("%d ",v[i]);
    }
}

输入图片此处描述

When I create a vector , let's say size of 5 , elements are 1,2,3,4,5 and I want to add at the location( for example index 2) the numbers 200 and 300 , the vector should look like 1 ,2 ,200,300,3,4,5.

#include <stdio.h>
#include <stdlib.h>

int main(){
    int v[] ={1,2,3,4,5,6,7};
    int n = sizeof(v)/sizeof(int);
    int location,element;
    printf("Enter location:");
    scanf("%d",&location);
    printf("Enter element:");
    scanf("%d",&element);
    for(int i = n - 1; i >= location;i--){
        v[i + 1] = v[i];
    }
    v[location] = element;
    for(int i = 0; i <= n;i++){
        printf("%d ",v[i]);
    }
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2025-01-22 05:03:42

您需要像这样循环

for(;;){
  int location,element;
  printf("Enter location:");

  scanf("%d",&location);

  if(location == -1)
     break;
  printf("Enter element:"); 
  scanf("%d",&element);
  for(int i = n - 1; i >= location;i--){
    v[i + 1] = v[i];
  }
  v[location] = element;
}   

,但是您的代码严重损坏,您正在添加到固定大小的向量。你不能这样做

在这段代码中

    for (int i = n - 1; i >= location; i--) {
        v[i + 1] = v[i];
    }

n是元素数量的计数,所以在你做的第一个循环中,

        v[n] = v[n-1]

       v[7] = v[6]

v[7]离开了数组的末尾(数组索引是0到6)那非常糟糕

You need to loop like this

for(;;){
  int location,element;
  printf("Enter location:");

  scanf("%d",&location);

  if(location == -1)
     break;
  printf("Enter element:"); 
  scanf("%d",&element);
  for(int i = n - 1; i >= location;i--){
    v[i + 1] = v[i];
  }
  v[location] = element;
}   

BUT you code is severely broken, you are adding to a vector thats of fixed size. you cannot do that

In this code

    for (int i = n - 1; i >= location; i--) {
        v[i + 1] = v[i];
    }

n is the count of number of elements, so on the first loop you do

        v[n] = v[n-1]

ie

       v[7] = v[6]

well v[7] is off the end of the array (array indexes are 0 to 6) Thats very bad

标点 2025-01-22 05:03:42

您声明了一个固定大小的数组,

int v[] ={1,2,3,4,5,6,7};

您无法放大它。

该循环

for(int i = 0; i <= n;i++){
    printf("%d ",v[i]);
}

因此,由于使用的索引值 n 超出了源数组的索引 [0, n) 的有效范围,因此

会调用未定义的行为。您需要动态分配数组,当您要添加一个元素时,您将需要重新分配数组。

这是一个演示程序。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( void )
{
    size_t n = 7;

    int *v = malloc( n * sizeof( int ) );
    memcpy( v, ( int [7] ){ 1, 2, 3, 4, 5, 6, 7 }, n * sizeof( int ) );

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", v[i] );
    }

    putchar( '\n' );

    size_t location = 0;

    printf( "Enter location: " );
    scanf( "%zu", &location );

    if ( n < location ) location = n;

    int element = 0;

    printf( "Enter element: " );
    scanf( "%d",&element );

    int *tmp = realloc( v, ( n + 1 ) * sizeof( int ) );
    
    if ( tmp != NULL )
    {
        v = tmp;

        memmove( v + location + 1, v + location, ( n - location ) * sizeof( int ) );
        v[location] = element;
        ++n;
    }

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", v[i] );
    }

    putchar( '\n' );

    free( v );
}    

程序输出可能类似于

1 2 3 4 5 6 7
Enter location: 2
Enter element: 200
1 2 200 3 4 5 6 7

You declared a fixed size array

int v[] ={1,2,3,4,5,6,7};

You can not enlarge it.

Thus this loop

for(int i = 0; i <= n;i++){
    printf("%d ",v[i]);
}

invokes undefined behavior due to using the index with the value n that is outside the valid range of indices [0, n) for the source array.

You need to allocate the array dynamically and when you are going to add one more element you will need to reallocate the array.

Here is a demonstration program.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( void )
{
    size_t n = 7;

    int *v = malloc( n * sizeof( int ) );
    memcpy( v, ( int [7] ){ 1, 2, 3, 4, 5, 6, 7 }, n * sizeof( int ) );

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", v[i] );
    }

    putchar( '\n' );

    size_t location = 0;

    printf( "Enter location: " );
    scanf( "%zu", &location );

    if ( n < location ) location = n;

    int element = 0;

    printf( "Enter element: " );
    scanf( "%d",&element );

    int *tmp = realloc( v, ( n + 1 ) * sizeof( int ) );
    
    if ( tmp != NULL )
    {
        v = tmp;

        memmove( v + location + 1, v + location, ( n - location ) * sizeof( int ) );
        v[location] = element;
        ++n;
    }

    for ( size_t i = 0; i < n; i++ )
    {
        printf( "%d ", v[i] );
    }

    putchar( '\n' );

    free( v );
}    

The program output might look like

1 2 3 4 5 6 7
Enter location: 2
Enter element: 200
1 2 200 3 4 5 6 7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文