zoukankan      html  css  js  c++  java
  • 翻转一个数组(c++实现)

    反转一个数组:

    其实STL中的vector有一个reverse函数便可以使用。

    #include<iostream>
    using namespace std;
    int* ReverseArray(int*orig,unsigned short int b)
    {
        unsigned short int a=0;
        int swap;
        for(a;a<--b;a++) //increment a and decrement b until they meet eachother
        {
            swap=orig[a];       //put what's in a into swap space
            orig[a]=orig[b];    //put what's in b into a
            orig[b]=swap;       //put what's in the swap (a) into b
        }
        return orig;    //return the new (reversed) string (a pointer to it)
    }
    
    int main()
    {
        const unsigned short int SIZE=10;
        int ARRAY[SIZE]={1,2,3,4,5,6,7,8,9,10};
        int*arr=ARRAY;
        for(int i=0;i<SIZE;i++)
        {
            cout<<arr[i]<<' ';
        }
        cout << endl;
        arr=ReverseArray(arr,SIZE);
        for(int i=0;i<SIZE;i++)
        {
            cout<<arr[i]<<' ';
        }
        cout<< endl;
    
        return 0;
    }

    结果:

  • 相关阅读:
    4.2.1 B
    4.1.1 A
    C
    A
    排序(sort qsort)
    晕,
    clipssubviews = clipstobounds
    scrollview once more,滑出来的 刚好等于 上下偏移的,
    关于 层的显示,
    水倒过来,倒过去,穷折腾啊,
  • 原文地址:https://www.cnblogs.com/simplepaul/p/7677408.html
Copyright © 2011-2022 走看看