Does "unique" function work in vector<char> ?

I thought to find unique elements from a string data is straightforward. But it turned out to be not!

The "unique" function definitely works well on vector<int>. But when I tried it on vector<char>, it seems it does not. I tested by "distance" between the beginning of the output vector and the iterator,it, returned from the unique function. The outputSize is still 8 as same as the input string "abacacde".

Why? Please help.

Thanks,

L



Below is my current output and code.
----------------------Output:
inputStr.begin()a
inputStr.end()e
*(it-1)=e
outputSize=8
---------------------Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

void stringFilter(const char *pInputStr, long lInputLen, char *pOutputStr)
{

char * inputStr2;
inputStr2=const_cast<char *>(pInputStr);

string inputStr,outputStr;
inputStr=string(pInputStr);

vector<char> inputVec(inputStr.begin(), inputStr.end());
vector<char> outputVec;
vector<char>::iterator it;

cout<<"inputStr.begin()"<<*inputStr.begin()<<endl;
cout<<"inputStr.end()"<<*(inputStr.end()-1)<<endl;

it=unique(inputVec.begin(), inputVec.end());

cout<<"*(it-1)="<<*(it-1)<<endl;
int outputSize;
outputSize=distance(inputVec.begin(),it);
cout<<"outputSize="<<outputSize<<endl;
}

int main(int argc, const char * argv[])
{

const char* inputChr;
char * outputChr;
long inputStrSize;

inputChr="abacacde";
inputStrSize=strlen(inputChr);

stringFilter(inputChr,inputStrSize,outputChr);


return 0;
The "unique" function definitely works well on vector<int>.

The "unique" function definitely works well with sorted data. Not so good without.
So it does not work well on a vector of letters?
try adding
 
    std::sort(inputVec.begin(), inputVec.end());
Thanks. So the answer is to sort the string before unique it.
Topic archived. No new replies allowed.