Hi i need help in c++ vectors ... if you have a vector lets say 1,4,5,6,4,6,7 and you have to remove the duplicate vectors i.e to bring it in the form

1,4,5,6,7

....

i tried the double nested loop and trying to get the first vector and comparing with the other vector loops and then if it matches it deletes the vector entery... ( dun knw if its the rite way to do or not )

void alternating_sum(vector<double>& a)  {
        double even=0;
        double odd=0;
        double sum=0;
        for (int i=0;i<a.size()/2;i++){
              v1=a[i];


                    for (int j=0; j<a.size()/2;i++){
                    v2=a[j];
                   if (v1==v2){  
                    double last_pos= a.size()-1;                          a[j]=a[last_pos];
                          a.pop_back();}

could someone help me .. i am just stuck on the part how could i match the first i entery with the other j entery and then delete if its the same or move on ... i tried the above code but gives out error some help would be nice

and could someone let me knw if i am doing it rite or not

Edited 3 Years Ago by Dani: Formatting fixed

ok this is where i have reached but does not work i.e does not give the correct output .. can someone help me asap need this for school

#include <iostream>
#include <vector>

using namespace std;

void alternating_sum(vector<double>& a)
{
    double even=0;
    double sum=0;

        for (int i=0;i<a.size();i++){
        even=a[i];
    
            for (int j=(i+1); j<a.size();i++){
            sum=a[j];
            if (a[i]==a[j]){
                int last= a.size()-1;
                a[j]=a[last];
                a.pop_back();}
                         else 
                cout<<a[i]<<" ";
                    }
            
}
}

int main()
{
   vector<double> a(10);
  
   a[0] = 1;
   a[1] = 4;
   a[2] = 9;
   a[3] = 16;
   a[4] = 9;
   a[5] = 7;
   a[6] = 4;
   a[7] = 9;
   a[8] = 11;

  


alternating_sum(a);
  

   return 0;
}

aint there any other way to do without erasing the values ???

how would u do it other than erasing it .... help would be nice

But the question here is, why not just erase it ? Is it a requirement ? BTW, you can get all the reference to vector methods here.

Would it be easier to remove duplicates if the vector was sorted first?

Definately. It would then require only a single loop to do away with the duplicates.

This article has been dead for over six months. Start a new discussion instead.