I have the following code which removes duplicates from an array:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

char main()
{
   char myarray[10] = {'a', 'b', 'b', 'b', 'b', 'c'};
   int myarraylength = 5;
   string holdprogram;

   vector< char > myvector(myarray, myarray + (myarraylength + 1));

   ostream_iterator< char > output(cout, " ");

   copy(myvector.begin(), myvector.end(), output);

   vector< char >::iterator endpoint;
   endpoint = unique(myvector.begin(), myvector.end());

   cout << endl;
   copy(myvector.begin(), endpoint, output);

   cin >> holdprogram;
}

What I'm looking to find out is what is the new size of the array once the duplicates are removed?

Recommended Answers

All 6 Replies

Subtract myvector.begin() from endpoint and you'll know the size of the vector after calling unique.

>> char main()
Interesting... Guess it's not as bad as void...

>> char main()
>Interesting... Guess it's not as bad as void...
No, it's just as bad.

The following code:
myarraylength = (myvector.begin() - endpoint);

Gives a value of -3 instead of 3. Why is that?

should be the other way around.

should be the other way around.

Awesome, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.