c++ - 从字符数组中消除重复项

标签 c++ visual-studio-2012

我必须使用指针和函数从字符数组中消除重复项。 我无法让它正常工作:

void del_copy(char *p){
int n = strlen(p);          
for (int i = 0; i < n; i++){           // Element that we compare
    for (int j = i + 1; j < n;j++){     //We compare p + i element with the rest of the elements
        if (*(p + i) == *(p + j)){        //If we find two similar, we eliminate that element by shifting
            for (int k = i; k < n; k++)
                *(p + k) = *(p + k + 1);
            }               
        }
    }
}

最佳答案

在你做一个转变后,长度会改变。但是您的 n 没有考虑到这一点。

关于c++ - 从字符数组中消除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27611432/

相关文章:

c++ - 左移计数 >= 字宽

visual-studio-2012 - 在哪里可以找到 makecert.exe? Visual Studio 旗舰版 2012

c# - 在 Windows 8 应用程序中用手指触摸滚动列表框的问题

c - 在 C 中调试断言失败,不确定是什么原因造成的

c# - 重命名项目路径后如何将现有项目添加到 Visual Studio 2012

c++ - ICP的保证,内部指标

c++ - 在 QTimer::singleShot 中删除它

c++ - 带有迭代器和自定义步长的循环结束条件

C++ 优化,使用 > 而不是 <=

c++ - VS 2012 模板错误