Deleting repeated values from a vector

2

Hello, people briefly my problem is to delete repeated values of a vector, here is my code and just below a brief explanation.

===================================================== ==========================

#include <iostream>
#include <iterator>

using namespace std;

int* unique( int *first, int *last);
int main(){

int A[] = { 1, 2, 1, 2, 3, 3, 1, 2, 4, 5, 3, 4, 5 };

//aplicar unique sobre A.

auto last = unique(std::begin(A), std::end(A));

//o comando abaixo deveria imprimir A com o conteudo 1, 2, 3, 4, 5.
for( auto i = std::begin(A); i != last; ++i){
    std::cout << *i << " ";
}

std::cout << endl;

//mostrar o novo tamanho de A, que seria 5.
std:: cout << ">>O comprimento logico de A apos o unique() eh: " << std::distance(std::begin(A), last) << std::endl;
std::cout << ">>O retorno indica a posição do elemento: " <<*last << std::endl;


    return 0;
}


int* unique( int *first, int *last){
auto slow = first+2;
    for (auto fast = first+2; fast != last; ++fast){
        for (auto i = slow; i != first; /*empty*/){
            if(*fast != *i ){
                swap(*slow++, *fast);
            }
            --i;
        }
    }
    return slow;
}

===================================================== ==========================

Well, basically I send two pointers indicating the beginning and the end of my vector, then I create two more pointers (slow and fast), the fast traverses the vector and tests if the current element is different from the one behind it slow, pushing the repeated values to the end of the vector and getting only the unique values at the beginning, the function returns slow, that is, indicates where the single values ends, the problem arises exactly in this part of the slow back test, the function is not returning the exact value, I wanted to do this test using the swap () function.

    
asked by anonymous 05.03.2017 / 16:55

0 answers