我知道我的问题对某些人来说可能很愚蠢,但我整天都在谷歌上搜索并尝试制定自己的解决方案,但我失败了..请帮助..
我需要从一个简单的字符串数组中打印所有唯一字符串。
例子:
输入:“嗨”“我的”“名字”“嗨”“土 bean ”“文本”“名字”“嗨”
输出:“我的”“马铃薯”“文本”
我只是将所有内容打印一次(“Hi”、“my”、“name”、“potato”、“text”),但我需要忽略数组中 2 倍或更多次的所有内容。
我的算法是: 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/