2017-04-06 67 views
0

所以我需要能够在C++中完成这项工作。我能做的就是使用“abaaacc”作为字符串,并且我得到了正确的答案,但是当“a b a a c c”在链接列表中时,我不知道如何解决它。谁能帮我用代码:只留下给定列表中未重复的元素。例如:(a b a a a c)给了我们(a b)

这里是我的代码

#include <iostream> 
using namespace std; 
const int SIZE=20; 
int main() 
{ 
    int numbs[SIZE], value, idx,n; 
    cout<<"PLease enter size of an array"<<endl; 
    cin>>n; 
    cout << "Please enter in a series of numbers "<<endl; 
    for(idx = 0; idx < n; idx++) 
     cin >>numbs[idx]; 


    cout<< numbs[0] << " "; 
for (int i = 1; i < n; i++) 
{ 
    bool matching = false; 
    for (int j = 0; (j < i) && (matching == false); j++)if (numbs[i] == numbs[j]) matching = true; 
    if (!matching) cout<< numbs[i] << " "; 
} 

} 

现在我希望它删除重复相邻,给我的这

像前1个拷贝,但使用号码,我怎么能够编辑我的代码来做到这一点。

+2

我猜的人可以帮你的代码 - 发布的代码,他们可以建议如何解决它... – yakobom

+0

的问题是,我不这样做很好用链接列表,我做它时,他们是一个字符串,但不作为列表 – Ahmad

+2

通常的程序是:您尝试自己解决它。你卡住了。你可以问一个具体的问题。 – moooeeeep

回答

0

我有一段时间,所以我试图解决这个问题。

这确实是一件棘手的事情。您需要关注边缘案例的第一个和最后一个项目,以及1-2个项目列表。 在两者之间需要同时迭代三个迭代器以在这些子集中间找到唯一项。而当你处理一个列表时,你需要解决丢失的随机访问迭代器。

我现在更习惯于Python,在这种情况下复杂的迭代很舒服,在这种情况下可以很好地使用拉链和切片。也许新的ranges可能已经被用来改善这个代码。也许我会试一试。

#include <list> 
#include <iostream> 

std::list<char> remove_adjacent_duplicates(std::list<char> input) { 
    std::list<char> output; 
    std::list<char>::iterator first = input.begin(); 
    if (first == input.end()) { 
     // no first, item, exit 
     return output; 
    } 

    // let second point to second element 
    std::list<char>::iterator second = input.begin(); 
    ++second; 
    if (second == input.end()) { 
     // no second item, insert first, then exit 
     output.push_back(*first); 
     return output; 
    } 

    // check first item 
    if (*first != *second) { 
     // first and second are different, insert first 
     output.push_back(*first); 
    } 

    // let third point to third item 
    std::list<char>::iterator third = input.begin(); 
    ++third; ++third; 
    // check items up until the last 
    while (third != input.end()) { 
     if (*first != *second && *second != *third) { 
      // the second item neither equals the first, nor the third 
      // insert second 
      output.push_back(*second); 
     } 
     // increment iterators 
     ++first; ++second; ++third; 
    } 

    // finally, check the last item 
    if (*first != *second) { 
     // the last two items differ, insert the latter 
     output.push_back(*second); 
    } 

    // done 
    return output; 
} 

void test_case(std::list<char> l) { 
    std::list<char> output = remove_adjacent_duplicates(l); 
    for (char i : l) { 
     std::cout << i << ' '; 
    } 
    std::cout << " -> "; 
    for (char i : output) { 
     std::cout << i << ' '; 
    } 
    std::cout << '\n'; 
} 

int main() { 
    test_case({'a'}); 
    test_case({'a', 'b'}); 
    test_case({'a', 'b', 'a', 'a', 'a', 'c', 'c'}); 
} 

输出是:

$ g++ test.cc -std=c++11 && ./a.out 
a -> a 
a b -> a b 
a b a a a c c -> a b 
+0

非常感谢你的努力,我能够理解代码 – Ahmad

0

够简单。首先你需要std::sort这个容器,然后你使用std::unique(加上erase)删除每个值的所有值,但除了一个以外。

+0

OP想要删除** pair **重复,这需要一个自定义的“唯一”。 – Jarod42

+0

std ::在链表上排序??? –

+0

那么,你可以对从列表初始化的'std :: reference_wrapper'的std :: vector'进行排序。或者只是从列表中构建一个向量,完成这项工作,然后构建一个新列表(如果坚持一个列表)。 –

相关问题