我知道我的问题对某些人来说可能很愚蠢,但我整天用谷歌搜索并尝试制定自己的解决方案,但我失败了..请帮助..
我需要从简单的字符串数组中打印所有唯一的字符串。
example:
输入:“嗨”“我的”“名字”“嗨”“土豆”“文本”“名字”“嗨”
输出:“我的”“土豆”“文本”
我只制作一次打印所有内容的函数(“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';
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)