如何从另一个元素前面的数组中放置一个元素。

时间:2018-01-06 10:43:43

标签: c++

如何将数组中的元素放在同一数组的另一个元素前面。(//并且不交换它们的位置!!) 对于前... 57364 35764(3前面3) 35674(6前面6) 34567(5前面4个)

4 个答案:

答案 0 :(得分:0)

通用的就地解决方案可能是:

#include<vector>
#include<cassert>
#include<algorithm>

// random iterator, the behaviour is undefined if first == second, or
// first and second do not belong to some valid range
template<typename RIter>
void move_in_front_of( RIter first, RIter second )
{
  std::iter_swap(first,second);

  if( first < second )
    std::rotate(std::next(first),second,std::next(second));
  else
    std::rotate(std::next(second),first,std::next(first));
}

int main()
{
  auto v1 = std::vector<int>{5,7,3,6,4};
  auto v2 = std::vector<int>{3,5,7,6,4};
  auto v3 = std::vector<int>{3,5,6,7,4};
  auto v4 = std::vector<int>{3,4,5,6,7};

  move_in_front_of( v1.begin()+2, v1.begin() );//3,5
  assert( v1 == v2 );
  move_in_front_of( v1.begin()+3, v1.begin()+2 );//6,7
  assert( v1 == v3 );
  move_in_front_of( v1.begin()+4, v1.begin()+1 );//4,5
  assert( v1 == v4 );
}

答案 1 :(得分:0)

此程序采用数组位置来确定前后元素。

// ...    
private Path mTempPath = new Path();
private RectF mTempRectF = new RectF();
private Rect mTempRect = new Rect();
// ...

public void requestFocusRange(int start, int end) {
    final Layout layout = getLayout();
    if (layout == null) {
        // probably rotating the screen, do nothing
        return;
    }

    layout.getSelectionPath(start, end, mTempPath);
    mTempPath.computeBounds(mTempRectF, false);
    mTempRectF.roundOut(mTempRect);

    // Fix the coordinates origin
    final int paddingLeft = getTotalPaddingLeft();
    mTempRect.offset(paddingLeft, getTotalPaddingTop());

    // Unnecessary - I'm just adding extra spacing to the focused rect
    mTempRect.inset(-paddingLeft, -paddingLeft);

    requestRectangleOnScreen(mTempRect, false);
}

答案 2 :(得分:-1)

可以优化..这次是用c ++编写的。 exchange_index是您要移动到insertion_index的数字的索引。

#include <iostream>
using namespace std;

int main() 
{
    int a[5] = {3,5,6,7,4};

    int exchange_index = 2;
    int exchange_val = a[exchange_index];
    int insertion_index = 3;

    if(exchange_index > insertion_index) {
        for(int i=exchange_index;i>insertion_index;i--) {
            a[i] = a[i-1];
        }
    } 
    else {
        for(int i=exchange_index;i<insertion_index;i++) {
            a[i] = a[i+1];
        }
    }

    a[insertion_index] = exchange_val;

    for (int i = 0; i < 5; i++) 
        cout << a[i];

    return 0;
}

答案 3 :(得分:-1)

试试这个代码! 。它工作正常。 我还附上了代码的屏幕截图。

#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
    // integer array
    int arr[10] = { 3, 1, 6, 78, 5, 3, 7, 90 };

    //size of the current array
    int n = 8;

    //num varaible is used to take the input of front element of the array that you want 
    //val varaible is used to take the input that you want to inserted in the array
    //flag variable indicates the front element 'num' is found in array or not 
    //index varaible is used to keep the index of array where 'num' value found
    int num, val, flag = 0, index;


    //before inserted , prints the array values
    cout << "\t\t Array values" << endl;
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << "\t";
    }


    //taking front value input from the user through keyboard 
    cout << "\nEnter front element of the array :";
    cin >> num;

    //taking inserted value input from the user
    cout << "\nEnter element that are inserted infront of " << num << " :";
    cin >> val;


    //finding the index of front value in the array
    for (int i = 0; i < n; i++)
    {
        if (arr[i] == num)
        {
            index = i;
            flag = 1;
            break;
        }
    }


    //inserted in the array infront of 'num' value
    if (flag == 1)
    {
        // if front value found in the array
        for (int i = n; i > index; i--)
        {
            arr[i] = arr[i-1];
        }
        arr[index] = val;
        cout << "\nInserted successfully ....!"<<endl;
    }
    else
    {
        // when front value not found in the array
        cout << "\nFront element not found ... !"<<endl;
    }


    // after successfull insertion , prints the array 
    cout << "\n\t\t Array values" << endl;
    for (int i = 0; i < n + 1; i++)
    {
        cout << arr[i] << "\t";
    }

    _getch();
    return 0;
}

Output of the code