2

视觉 C++ 问题

你好,

我有 3 个元素的数组,我想将其元素向右移动并用“SHIFTED”字符串替换移动的索引单元格,这应该循环直到所有单元格都有“SHIFTED”字符串。


例如:

int a[x]={0,1,2};

初始索引和元素顺序:

[0]=0
[1]=1
[2]=2

应该成为:

第一个循环:

 [0]=SHIFTED
 [1]=0
 [2]=1

第二循环:

 [0]=SHIFTED
 [1]=SHIFTED
 [2]=0

第三循环:

 [0]=SHIFTED
 [1]=SHIFTED
 [2]=SHIFTED

我知道我可以用 memmove() 做到这一点,但我不想在其中使用任何函数。

你能帮帮我吗?这是我的工作:

#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
using namespace std;

int const ARRAY_SIZE=3;

int main()
{
 int Array[ARRAY_SIZE];
 int iniPostion,newPostion;
 string x="SHIFTED";
 for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++)
 {
  Array[iniPostion] = iniPostion;
  cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
 }
 cout << endl;
 for(newPostion=0; newPostion<ARRAY_SIZE; newPostion++)
 {
  Array[newPostion]=newPostion;
  cout << "Cell [" << newPostion << "]     New Element is: (";
  if(Array[newPostion-1]<0)
  {
   cout << x << ")\n";
  }
  else
  {
   cout << Array[newPostion-1] << ")" << endl;
  }
 }
 return 0;
}
4

6 回答 6

3

正如彼得在他的回答中提到的那样,您不能将字符串分配给 int。我假设SHIFTED-1。所以每次你换班时,你都会带来一个-1创造的差距。

你需要两个循环。外部循环迭代N(3) 次,内部循环从数组的末尾开始并将(n-1)th元素复制到nth位置:

for(int count = 0;count < N;count++){
    for(newPostion=ARRAY_SIZE-1; newPostion > count;newPostion--)
        Array[newPostion]=Array[newPostion - 1]; // copy
    Array[newPostion]= -1; // fill the gap.

    // print.
    for(iniPostion=0; iniPostion<ARRAY_SIZE; iniPostion++) {
        cout << "Cell [" << iniPostion << "] Initial Element is: (" << Array[iniPostion] << ")" << endl;
    }
    cout<<endl;
}

样品运行:

# g++ a.cpp && ./a.out
Cell [0] Initial Element is: (0)
Cell [1] Initial Element is: (1)
Cell [2] Initial Element is: (2)

Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (0)
Cell [2] Initial Element is: (1)

Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (0)

Cell [0] Initial Element is: (-1)
Cell [1] Initial Element is: (-1)
Cell [2] Initial Element is: (-1)
于 2010-03-07T14:58:56.567 回答
2

有点不清楚,您如何期望在整数数组中包含字符串(“SHIFTED”)。

但是,对于此操作,您可以使用旋转算法:

#include <iostream>
#include <algorithm>
#include <string>

int const ARRAY_SIZE=3;

void print(std::string* array) {
    for (int i = 0; i != ARRAY_SIZE; ++i) {
        std::cout << array[i] << ' ';
    }
    std::cout << '\n';
}

int main()
{
    std::string Array[ARRAY_SIZE] = {"0", "1", "2"};
    print(Array);

    //the last item ends up at the beginning of the array
    std::rotate(Array, Array + ARRAY_SIZE - 1, Array + ARRAY_SIZE);

    //now overwrite the item that used to be last
    Array[0] = "SHIFTED";
    print(Array);
    return 0;
}

使用合适的容器可能会更简单,更有效,例如您可以std::deque使用最后一个值和新值。std::listpop_backpush_front

于 2010-03-07T14:58:39.817 回答
1

我有 3 个元素的数组,我想将其元素向右移动并用“SHIFTED”字符串替换移动的索引单元格,这应该循环直到所有单元格都有“SHIFTED”字符串。

这根本没有意义。你有一个数字数组(整数),当然不能包含字符串。您可以做的是例如插入 0 或 -1 来表示移位的元素。

移位本身可以通过std::rotate操作轻松实现。

但是既然所有元素最终都包含相同的东西,为什么不直接分配它们而不进行所有的转换呢?

于 2010-03-07T14:57:31.647 回答
1

这是我在普通旧 C 语言中的简单解决方案

#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {

    int MAX_LEN = 11;
    int numOfShifts = 1;
    if ( argc > 1 ) {
        numOfShifts = atoi(argv[1]);
    }
    printf("Number of shifts = %d\n",numOfShifts);

    int arr[] = { 0,1,2,3,4,5,6,7,8,9,10 };
    int i;
    int n; // number of shifts index

    for ( n = 0; n < numOfShifts; n++ ) {
        for ( i = MAX_LEN - 1; i >= 0; i-- ) {
            if ( i == 0 ) {
                arr[i] = -1;
            } else {
                arr[i] = arr[i-1];
            }
        }
    }

    // output
    for( i = 0; i < MAX_LEN; i++ ) {
        printf("Arr[%d] = %d\n", i, arr[i]);
    } 

}
于 2010-03-07T15:56:56.260 回答
0

这家庭作业的味道...

您是否有理由不能只跟踪您完成了多少班次并在打印时将其考虑在内?

#include <iostream>

int const ARRAY_SIZE=3;

class Shifter
{
    public:
        Shifter(const int* array_, size_t size_)
        : array(array_), size(size_), shifts(0) {}
        void Shift(size_t steps = 1) { shifts += steps; if(shifts > size) shifts = size; }
        void Print(std::ostream& os) const;
    private:
        const int* array;
        size_t size;
        size_t shifts;
};

void Shifter::Print(std::ostream& os) const
{
    for(size_t i = 0; i < size; ++i)
    {
        os << "Cell [" << i << "]  = ";
        if(i < shifts)
            os << "SHIFTED";
        else
            os << array[i - shifts];
        os << std::endl;
    }
}

std::ostream& operator <<(std::ostream& os, const Shifter& sh)
{
    sh.Print(os);
    return os;
}

int main(void)
{
    // Initialize the array.
    int a[ARRAY_SIZE];
    for(size_t iniPostion=0; iniPostion < ARRAY_SIZE; iniPostion++)
        a[iniPostion] = iniPostion;
    Shifter sh(a, ARRAY_SIZE);
    std::cout << "Initial contents:" << std::endl;
    std::cout << sh << std::endl;

    // Do the shifts.
    for(size_t newPostion = 0; newPostion < ARRAY_SIZE; newPostion++)
    {
        std::cout << "Shift by 1..." << std::endl;
        sh.Shift();
        std::cout << sh << std::endl;
    }

    return 0;
}
于 2010-03-07T15:30:57.280 回答
0

您可以简单地移动内存。

// shift down
int a[ 10 ];
memmove( a, a +1, sizeof( a ) -sizeof( a[ 0 ] ) );
于 2012-04-17T15:50:28.143 回答