c++ - 如何从数组 C++ 中获取唯一字符串

标签 c++ string sorting unique

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

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

例子:

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

输出:“我的”“马铃薯”“文本”

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

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

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

.. if(array[i]!=array[i+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';
    }
}

关于c++ - 如何从数组 C++ 中获取唯一字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34245994/

相关文章:

java - 使用 SharedPreferences 保存字符串

mysql - 测验布局改进

Javascript 通过 bool 属性对对象数组进行排序

c++ - 将 int 转换为具有零填充(前导零)的 QString

c++ - float 末尾的点是否表示缺乏精度?

c++ - 在不移动末尾的情况下删除文件中间的字节?

python - 按数字和字母顺序对两个元素元组的列表进行排序

c++ - 如何使子控件处理父 CView 的加速器命令

python - python中可变数量的参数

c# - 比较字符串