gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:55:28 25 4
gpt4 key购买 nike

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

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

例子:

输入:“嗨”“我的”“名字”“嗨”“土 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com