0

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?

4
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by tehprince
0

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

0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.