C++ 消除字符数组中的重复项

C++ 消除字符数组中的重复项,c++,visual-studio-2012,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 ele

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

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);
            }               
        }
    }
}
void del_副本(char*p){
int n=strlen(p);
对于我们比较的(inti=0;i
执行移位后,长度会发生变化。但是你的n没有考虑到这一点。

在你移动之后,长度会改变。但是您的n没有考虑到这一点。

这里有一个简单的算法:

让我们成为空集(到目前为止遇到的字符)
让结果为空字符串
对于输入字符串中的每个字符c(
如果c不在s中(
在s和
将c附加到结果的末尾
)
)
结果就是结果

对于具有8位字节的普通机器上的
char
类型,您可以使用
std::bitset
作为set
s


另一个简单的算法是,如果不需要保持顺序,则首先对字符串进行排序。然后您可以扫描它并查找重复项。

这里有一个简单的算法:

让我们成为空集(到目前为止遇到的字符)
让结果为空字符串
对于输入字符串中的每个字符c(
如果c不在s中(
在s和
将c附加到结果的末尾
)
)
结果就是结果

对于具有8位字节的普通机器上的
char
类型,您可以使用
std::bitset
作为set
s


另一个简单的算法是,如果不需要保持顺序,则首先对字符串进行排序。然后你就可以扫描它并寻找重复项。

这是我对这个问题的看法。它基本上使用一个查找表来检查字符是否以前见过,但是否已模板化。您可以使用任何类型的
value\u type

#include <algorithm>
#include <unordered_map>

template <typename Container>
Container unique_all(Container const& c) {
  Container out;
  ::std::unordered_map<typename Container::value_type,::std::size_t> lookup;
  ::std::copy_if(c.begin(), c.end(),
      ::std::back_inserter(out),
      [&](typename Container::value_type ch) {return !lookup[ch]++;}); 
  return out;
}

这是我对这个问题的看法。它基本上使用一个查找表来检查字符是否以前见过,但是否已模板化。您可以使用任何类型的
value\u type

#include <algorithm>
#include <unordered_map>

template <typename Container>
Container unique_all(Container const& c) {
  Container out;
  ::std::unordered_map<typename Container::value_type,::std::size_t> lookup;
  ::std::copy_if(c.begin(), c.end(),
      ::std::back_inserter(out),
      [&](typename Container::value_type ch) {return !lookup[ch]++;}); 
  return out;
}
要修复的样本

void del_copy(char *p){
    char c;
    for (int i = 0; c=*(p+i); i++){
        char *p2, *p3;
        for(p2 = p+i+1, p3 = p2; *p3; ++p3){
            if(*p3 != c)
                *p2++ = *p3;
        }
        *p2 = '\0';
    }
}
要修复的样本

void del_copy(char *p){
    char c;
    for (int i = 0; c=*(p+i); i++){
        char *p2, *p3;
        for(p2 = p+i+1, p3 = p2; *p3; ++p3){
            if(*p3 != c)
                *p2++ = *p3;
        }
        *p2 = '\0';
    }
}

我也设法修复了自己的代码。谢谢大家的支持。 我不得不在一段时间内改变if循环,并在ScottMcP MVP上减少de lenght

void del_copy(char *p){
int n = strlen(p);          
for (int i = 0; i < n-1; i++){           // Element that we compare
    for (int j = i + 1; j < n;j++){     //We compare p + i element with the rest of the elements
        while(*(p + i) == *(p + j)){        //If we find two similar, we eliminate that element by shifting
            for (int k = j; k < n; k++)
                *(p + k) = *(p + k + 1);
            n--;                             //decrement n because the lenght if smaller now
        }               
    }
}
}
void del_副本(char*p){
int n=strlen(p);
对于我们比较的(inti=0;i
我也设法修复了自己的代码。谢谢大家的支持。 我不得不在一段时间内改变if循环,并在ScottMcP MVP上减少de lenght

void del_copy(char *p){
int n = strlen(p);          
for (int i = 0; i < n-1; i++){           // Element that we compare
    for (int j = i + 1; j < n;j++){     //We compare p + i element with the rest of the elements
        while(*(p + i) == *(p + j)){        //If we find two similar, we eliminate that element by shifting
            for (int k = j; k < n; k++)
                *(p + k) = *(p + k + 1);
            n--;                             //decrement n because the lenght if smaller now
        }               
    }
}
}
void del_副本(char*p){
int n=strlen(p);
对于我们比较的(inti=0;i
std::unique
浮现在脑海中。难道不可能只添加一点“使用指针和函数”吗?您需要更好地定义问题。给出输入和输出示例。
aaabbaaa
应该变成
aba
?如果它变成
ab
?创建一个
char-tbl[1和顺便说一句,选择一种语言。它可以显著影响收到的答案。我想到了可能重复的
std::unique
。难道不可能只添加一点“使用指针和函数”吗?你需要更好地定义问题。给出输入和输出示例。如果
aaabbaaa
变成
aba
?如果变成
ab
?创建一个
char-tbl[1的表格,顺便说一句,选择一种语言。它可以显著影响收到的答案。可能重复