c++ 将数组元素向左移动

问题描述 投票:0回答:6

我最近才开始学习 C++,并且在移动数组元素以删除空/空元素时遇到困难

char *aBlock;
aBlock = new char[100];

int main(int argc, char **argv)
{
aBlock[20] = 'a'; // fill array with test data.
aBlock[10] = 's';
aBlock[30] = 'd'; // Excepted output: This test data should be shifted to the start of array

// Consider aBlock contains data, with random empty elements
    for(int i=1; i <= aBlock.length(); i++) {
        if(aBlock[i-1] == 0) {
            aBlock[i-1] = aBlock[i];
            aBlock[i] = 0;
        }
    }

    return 0;
}

编辑: 修复了代码拼写错误和错误的变量名称,将“==”更改为“=”。 仍然没有达到预期的效果。

c++ arrays shuffle shift
6个回答
0
投票

如果我理解正确,您想要移动数组开头的非零元素。您可以使用

std::remove_if
来执行此操作,并将其余元素设置为 0。

std::fill(
   std::remove_if(std::begin(aBlock), std::end(aBlock), [](char const c) {return c == '\0'; }),
   std::end(aBlock),
   0);

更新:

由于数组是动态分配的,因此您需要进行一些小更改:

std::fill(
   std::remove_if(&aBlock[0], &aBlock[100], [](char const c) {return c == '\0'; }),
   &aBlock[100],
   0);

0
投票

运算符

==
检查相等性,必须使用
=
来赋值。

memBlock[i-1] = memBlock[i];

C++ 中的数组没有像

.length()
这样的成员。它们不是课程。

for(int i=1; i <= 100; i++)
                  ^^^

0
投票

如果您在编译时知道大小,请使用 std::array(如果可用)。然后你可以执行“.size()”:)此外,如果你有几个连续的零,你的代码将不起作用。每个元素最多向左移动一次,这显然不足以达到您想要的结果。您需要做的是跟踪一个单独的“输出”索引/迭代器,它接收您遇到的任何非零值,然后递增:

std::array<char, 100> aBlock;
aBlock[10] = 's';
aBlock[20] = 'a';
aBlock[30] = 'd';

auto output = aBlock.begin(); // or aBlock if you don't have std::array;
for (auto input = aBlock.begin(); input != aBlock.end(); ++input)
{
  if (*input != 0)
  {
    if (output != input)
    {
      *output = input;
      *input = 0;
    }
    ++output;
  }
}

这应该可以解决问题。


0
投票
int arrsize = 100;

...

int i, j;
for(i = 0, j = 0; i < arrsize ; i++) {
    if(memBlock[i] != 0 && i != j) {
        memBlock[j++] = memBlock[i];
        memBlock[i] = 0;
    }
}

旁注:全球空间的新内容?那么删除[]在哪里?


0
投票

改变

    memBlock[i-1] == memBlock[i];

    memBlock[i-1] = memBlock[i];

“==”是我认为的问题。

使用

     if(aBlock[i-1] != 0)

0
投票

如果设置静态数组,测试会更容易,这样您就知道结果应该是什么样子。

而不是使用原始数组。我选择使用 .push_back() 创建新数组,然后操作新数组。会员功能; .erase,很挑剔,似乎不喜欢改变原始向量数组。

在我看来,这有点卡顿,哈哈,这不是我期望的最终结果代码的样子,但它确实有效。我曾想象每次迭代只是替换或交换数字,但我无法让它工作......

#include <iostream>
#include <vector>



int main()
{
    std::vector<int> array1 = { 1, 2, 3, 4, 5 };
    std::vector<int> array2;

    //each integer above 0 will "shift" the array1 to the left by one, so if you enter '0' or '5' it will either not move or move so it's back to the same order it started in
    int howmany;
    std::cout << "Shift left how many times?: ";
    std::cin >> howmany;

    //first part that always iterates will place all contents of array1 into array2
    for (int i = 0; i < array1.size(); i++) {
            array2.push_back(array1[i]);

            //if statement enacts when the loop completes, if (-1) is not added to the end of size it will not trigger
            if (i == array1.size() - 1) {
                //erases from the beginning of array1 to X which is set by (+ howmany) a variable controlled by the user, will shift left X times
                array2.erase(array2.begin(), array2.begin() + howmany);
                
                //again, using the howmany variable, add to the end what was erased from the beginning, starting at array1[0]
                for (int i = 0; i < howmany; i++) {
                    array2.push_back(array1[i]);
                }    
            }
    }

    //output for loop for the newly created array2 from contents of array
    for (int i = 0; i < array2.size(); i++) {
        std::cout << array2[i] << std::endl;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.