移位数组元素+1

问题描述 投票:-2回答:4

我已经为6个元素创建了数组,我想在0索引处添加新元素,并希望将所有+1移位,所以我如何在for循环中执行此操作或对大型数组执行任何简单的方法

#include <iostream>
    using namespace std;

    main(){
        int shift;
        int shift2;
        int shift3;
        int shift4;
        int shift5;

        int array[6]={1,2,3,4,5};
        shift=array[0];
        shift2=array[1];
        shift3=array[2];
        shift4=array[3];
        shift5=array[4];

            array[1]=shift ;
                array[2]=shift2;
                    array[3]=shift3 ;
                        array[4]=shift4 ;
                            array[5]=shift5 ;

            array[0]=90;

            for(int i=0 ; i< 6; i++){


            cout<<"array value at index "<<i<<" is "<<array[i]<<endl;


        }




    }
c++ arrays loops for-loop
4个回答
1
投票

首先,您应将所有数组项从最后一项移开。之后,只需覆盖索引为0

的第一个数组项
#include <iostream>

int main() {
    const int ARRAY_SIZE = 6;
    int array[ARRAY_SIZE] = {1, 2, 3, 4, 5, 0};
    for(int idx = ARRAY_SIZE - 1; idx > 0; --idx)
    {
        array[idx] = array[idx - 1];
    }
    array[0] = 10;

    for(int idx = 0; idx < ARRAY_SIZE; ++idx)
    {
       std::cout << "Array value at index " << idx << " is " << array[idx] << std::endl;
    }
}

输出:

Array value at index 0 is 10
Array value at index 1 is 1
Array value at index 2 is 2
Array value at index 3 is 3
Array value at index 4 is 4
Array value at index 5 is 5

0
投票

如果您读过memmove和memcpy,则差异可能为您提供线索,说明为什么使用1个临时对您不起作用。


0
投票

您可以std::rotate,然后覆盖第一个元素

#include <algorithm>
#include <iostream>
using namespace std;

int main()
{
    int array[6] = { 1, 2, 3, 4, 5 };
    std::rotate(std::begin(array), std::prev(std::end(array)), std::end(array));
    array[0] = 90;
    for (const auto &el: array) {
        std::cout << el << '\n';
    }
}

输出:

90
1
2
3
4
5

0
投票

对整数数组执行任务的一种简单方法是使用标准C函数memmove。例如

#include <iostream>
#include <cstring>

int main() 
{
    int array[] = { 1, 2, 3, 4, 5 };
    const size_t N = sizeof( array ) / sizeof( *array );

    std::memmove( array + 1, array, ( N - 1 ) * sizeof( int ) );

    array[0] = 90;

    for ( const auto &item : array ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

程序输出为

90 1 2 3 4 

另一种方法是使用标准算法,例如std::rotate

#include <iostream>
#include <iterator>
#include <algorithm>

int main() 
{
    int array[] = { 1, 2, 3, 4, 5 };
    const size_t N = sizeof( array ) / sizeof( *array );

    std::rotate( std::begin( array ), std::prev( std::end( array ) ),
                        std::end( array ) );

    array[0] = 90;

    for ( const auto &item : array ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

程序输出为

90 1 2 3 4 

如果使用for循环,则可以使用标准函数std::exchange进行以下查找。

#include <iostream>
#include <utility>

int main() 
{
    int array[] = { 1, 2, 3, 4, 5 };
    const size_t N = sizeof( array ) / sizeof( *array );

    int tmp = 90;

    for ( size_t i = 0; i < N; i++ )
    {
        tmp = std::exchange( array[i], tmp );
    }

    for ( const auto &item : array ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

再次显示程序输出为

90 1 2 3 4 

不使用任何标准功能,程序看起来就像

#include <iostream>

int main() 
{
    int array[] = { 1, 2, 3, 4, 5 };
    const size_t N = sizeof( array ) / sizeof( *array );

    for ( size_t i = N; i-- > 1;  )
    {
        array[i] = array[i-1];
    }

    array[0] = 90;

    for ( const auto &item : array ) std::cout << item << ' ';
    std::cout << '\n';

    return 0;
}

程序输出再次是

90 1 2 3 4 
© www.soinside.com 2019 - 2024. All rights reserved.