Excuse me does anyone here know how can we remove duplicates from a string using unique func or any other func?
for example if i want to turn "fdfdfddf"
into "df"
I wrote the code below but it seems it doesn't work
#include <bits/stdc .h>
using namespace std;
int main()
{
vector<int>t;
int n;
cin>>n;
string dd;
vector<string>s;
for(int i=0;i<n;i )
{
cin>>dd;
s.push_back(dd);
sort(s[i].begin(),s[i].end());
unique(s[i].begin(),s[i].end());
cout<<s[i]<<"\n";
}
}
CodePudding user response:
According to the documentation, unique
:
Eliminates all except the first element from every consecutive group of equivalent elements from the range [first, last) and returns a past-the-end iterator for the new logical end of the range.
If you want to get rid of the excessive elements, you have to do that explicitly, e.g., by calling erase
(as in the example in the documentation):
auto last = std::unique(s[i].begin(), s[i].end());
s[i].erase(last, s[i].end());
CodePudding user response:
int main()
{
string inputstr ,outstr;
cin>>inputstr;
outstr.append(inputstr,0,1);
for (int i = 0; i < inputstr.size() ; i )
{
int flag = 0;
for (int j = 0; j < outstr.size(); j )
{
if (inputstr[i] == inputstr[j])
{
flag = 1;
break;
}
}
if (flag == 0)
outstr.append(inputstr,i,1);
}
cout << outstr;
return 0;
}