【问题标题】:How to get unique strings from array c++如何从数组c ++中获取唯一的字符串
【发布时间】:2016-03-18 16:36:17
【问题描述】:

我知道我的问题对某些人来说可能很愚蠢,但我整天都在谷歌上搜索并尝试制定自己的解决方案,但我失败了..请帮忙..

我需要从一个简单的字符串数组中打印所有唯一的字符串。

示例:

输入:“嗨”“我的”“名字”“嗨”“土豆”“文本”“名字”“嗨”

输出:“我的”“土豆”“文本”

我只做一次打印所有内容的函数(“Hi”、“my”、“name”、“potato”、“text”),但我需要忽略数组中 2 倍或更多次的所有内容。

我的算法是: 1. 冒泡排序

  1. 使用基本的 for 和 if 仅打印排序序列中的最后一个字符串

.. if(array[i]!=array[i+1]) //做点什么...

【问题讨论】:

标签: c++ string sorting unique


【解决方案1】:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;
int main()
{
    vector<std::string> v = {
        "Hi", "my", "name", "Hi", "potato", "text", "name", "Hi",
    };

    sort(v.begin(), v.end());
    for (auto a = v.begin(), b = a; a != v.end(); a = b) {
        b = find_if(b, v.end(), [&](string s) {return *b != s;});
        if (distance(a, b) == 1)
                cout << *a << '\n';
    }
}

【讨论】:

    【解决方案2】:

    更新:我误解了这个问题,现在它可以作为问题的输出 简单地说,您可以计算每个字符串的出现次数并仅打印出现的字符串。 时间复杂度:O(N^2) 这是代码

    #include<iostream>
    #include<set>
    #include <string>
    #include <vector>
    using namespace std;
    int main()
    {
        int n; // number of strings you want in your array
        cin >> n;
        string t; // get t and push back it in the vector
        vector <string> words; //we use vector to store  as we will push back them
        for(size_t i = 1;i <= n;i++)
        {
            cin >> t;
            words.push_back(t);
    
        }
        for(int i = 0;i < words.size();i++)
        {
            int cnt = 0;
            for(int j = 0;j < words.size() && cnt < 2;j++)
            {
                if(words[i] == words[j])
                    cnt++;
            }
            if(cnt == 1) //its unique..print it
                cout << words[i] <<endl;
        }
    
    }
    

    【讨论】:

    • 嗨,谢谢,但这不是......我的问题不同......我需要完全忽略数组中的所有字符串,这些字符串在数组中出现 2 次或更多次......所以,只打印来自大批。再举个例子:输入:“hi”“hello”“hi”,输出只有“hello”,因为它在数组中只有1次......
    • 查看我的更新答案@Jiri Hlavik,抱歉造成误解。
    • 虽然代码确实可以正常工作,但我强烈建议对这些类型的循环繁重的任务坚持使用 STL 算法,因为它们更不容易出错并且更具可读性!
    • @Brian Rodriguez 我不明白你所指代码中的具体内容是什么?
    • 您使用的 for 循环可以替换为 &lt;algorithm&gt; 标头中的函数。有关示例,请参阅我的答案
    【解决方案3】:
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <vector>
    
    int main()
    {
        std::vector<std::string> words{"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi"};
    
        std::sort(words.begin(), words.end());
        for (auto curr = words.begin(); curr != words.end(); ) {
            // If working w/ few duplicate words:
            auto next = std::find_if(
                curr + 1, words.end(), [&](const auto& s) { return s != *curr; }
                );
            /* If working w/ many duplicate words:
            auto next = std::upper_bound(curr + 1, words.end(), *curr); */
            if (std::distance(curr, next) == 1) {
                std::cout << *curr++ << '\n';
            } else {
                curr = next;
            }
        }
    }
    

    Demo


    std::sortstd::upper_boundstd::find_ifstd::distance

    【讨论】:

    • @graham 我考虑过,但这会完成我需要完成的所有工作,而无需实际挑选出重要的元素,所以我选择自己做这项工作。
    • 不明白。 unique_copy 正是需要的。添加了我的答案。
    • unique_copy 复制相等元素的每个子范围的第一个元素。 OP 要求收集在该范围内仅出现一次的元素。
    【解决方案4】:
    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <string>
    #include <vector>
    
    int main()
    {
        std::vector<std::string> words{"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi"};
        std::vector<std::string> out;
    
        std::sort(words.begin(), words.end());
        std::unique_copy(words.begin(), words.end(), std::back_inserter(out));
        std::set_difference(words.begin(), words.end(), out.begin(), out.end(), std::ostream_iterator<std::string>(std::cout, " "));
    }
    

    注意:我在手机上和床上写这篇文章时未经过测试。

    【讨论】:

    • 你可以剪掉out向量,直接把std::unique_copy剪成std::ostream_iterator!此外,OP 希望单词只出现一次,因此在 unique_copy 之后需要额外的工作
    • 如果你走这条使用unique_copy 的路线,你将不得不使用set_difference 来查找重复项,然后在这些集合之间使用set_difference 来查找单次出现的项目。
    • 或根据元素计数从唯一性中删除/删除。
    • @BrianRodriguez 啊,是的。你说的对。我以为unique_copy返回了曾经列出的那些。
    【解决方案5】:
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    int i,j;
    string ss;
    cin>>ss;
     for(i=0;i<ss.length();i++)
    {
        for(j=1;j<ss.length();j++)
        {
         if(ss[i]==ss[j]&&i!=j)
           {
            ss[i]=' ';
           }
        }
    }
        for(i=0;i<ss.length();i++)
    {
        if(ss[i]!=' ')cout<<ss[i]<<" ";  /// for unique element print
    }
    return 0;
    

    }

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案6】:
    #include<iostream>
    using namespace std;
    int main()
    {
    int n;
    cin>>n;
    int a[n],i,j,k;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=1;j<n;j++)
        {
         if(a[i]==a[j]&&i!=j)
           {
            a[i]=0;
           }
        }
    }
    for(i=0;i<n;i++)
    {
        if(a[i]!=0)cout<<a[i]<<" ";
    }
    

    }

    【讨论】:

    • 问题是关于strings,而不是ints。另外,你为什么发布两个答案?除此之外,仅代码的答案并不是很有帮助。它鼓励人们从网上复制和粘贴,而不是学习如何点他的。你能edit你想保留的答案并解释一下代码吗?
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2013-07-09
    • 2021-05-10
    • 2021-06-25
    相关资源
    最近更新 更多