elements in vector

I need help with iteration of vector.
How can I erase element from the vector if there is already the same element in that vector?

Any help would be much appreciated!
what do you mean by
if there is already the same element in that vector?
sorry, I meant if elements have the same value, for example integers in vector: 1, 2, 2, 3, 4, 5, 5
how can I erase or remove the ones that have duplicates, to get an output like: 1,2,3,4, 5
Last edited on
you actually erase place holders (instead of values).
find which values are duplicate and erase corresponding places.
use a loop.
erase from end such as vec[6], vec[2]

EDIT: or copy all unique members to a new vector, and delete the whole old vector by vect.clear();
Last edited on
yes, but what if have populated that vector with inputs from user, and I don't know how many elements or what values those elements have, how would I erase then the elements that have the same value?
I need to iterate vector and somehow loop it to remove those elements, but I don't know how.
if you do it on the go, then dont add a value if thats already in the vector.
how would I do that in an example like the this one below, where user enters numbers, and then they are taken in to vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cout << "Do you want to write down any number? " << flush;
    cin >> answer;

    while (answer == "yes")
    {
        cout << "Enter number: " <<flush ;
        cin >> number;

        numbers.push_back (number);

        cout << "More numbers, yes or no? " << flush;
        cin >> answer;
    }
        {for (vector<int>::iterator   i = numbers.begin();
                                        i!= numbers.end();
                                        ++i)
    {
        cout << "You have: "<<endl;
        cout << *i << endl;
    }
Last edited on
use this
1
2
3
4
5
6
for(int i=size-2, k=vect[size-1]; i>=0; i--)
{
	if(vect[i]==k)
	//erase vect[i]
	else vect[i] = k;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
using namespace std;

int main( )
{
	int number;
	char answer;
	bool b;
	vector<int> vect;
	cout << "Do you want to write down any number? (y/n)" << flush;
    cin >> answer;

    while (answer == 'y')
    {
		b= true;
        cout << "Enter number: " <<flush ;
        cin >> number;
		for (int i=0; i<vect.size(); i++)
		{
			if (vect[i]==number) b= false;
		}

       if(b==true) 
		   vect.push_back (number);

        cout << "More numbers, yes or no? " << flush;
        cin >> answer;
    }

	cout << "\n\n";

	for(auto x: vect)
		cout << x << " "; //show all numbers
    
	cout << "\n\n";

return 0;
}
do I need to determine the size of the vector first in order to loop this?
I am sorry, I am total begginer and I don't know how to implement this in to the code above.
thank you so much for your help, I have tried the loop for erasing elements, works fine, but I get errors for this piece of code:

1
2
3
4
5
6
7
8
9
cout << "\n\n";

	for(auto x: vect)
		cout << x << " "; //show all numbers
    
	cout << "\n\n";

return 0;
}

line 32 error: error x does not name a type
line 35 error: expected ; before cout

sorry for being pain in the ass.
I have solved it with this piece of code:

1
2
3
cout<< "You have: ";
    ostream_iterator<int> out_it (cout,", ");
    copy ( vect.begin(), vect.end(), out_it );


Thank you again anup30 for all your help!

I just have one more question, I noticed that if user enters character instead of number, when he is asked to, program goes through endlees loop, and I need to exit it. How can this be avoided?
How can this be avoided?


^^^^while loop, line 14 to 29
First, that for (int x: vect) cout << x << " "; is a range-based for syntax, which requires C++11 support from the compiler. Furthermore, the keyword auto has different meaning in C++11 than what it had before. Which compiler do you use?


Lets assume that you already have a vector<Foo> vect with some values in it. First approach:
1
2
3
std::sort( vect.begin(), vect.end() );
auto pend = std::unique( vect.begin(), vect.end() );
vect.erase( it, vect.end() );

However, the order of values did change.

A no-sort version 1:
1
2
3
4
5
6
7
8
auto pbegin = vect.begin();
auto pend = vect.end();
while ( pbegin != pend ) {
  const auto value { *pbegin };
  pend = std::remove( pbegin+1, pend, value );
  ++pbegin;
}
vect.erase( pend, vect.end() );

A no-sort version 2:
1
2
3
4
5
6
7
8
9
10
std::vector<Foo> temp;
tem.reserve( vect.size() );
std::set<Foo> known;
for ( auto x : vect ) {
  if ( 0 == known.count( x ) ) {
    known.insert( x );
    temp.push_back( x );
  }
}
vect.swap( temp );



I noticed that if user enters character instead of number

The formatted input for number does not find any number-like charaters and sets the fail-bit for the stream. As long as the bit is there, all future input will fail automatically. You have to tell the stream that it is ok, and then remove from the stream all offending characters before next input.
1
2
3
4
5
6
std::cin >> number;
if ( ! cin ) {
  cin.clear();
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  std::cin >> number;
}



(I trust that you can seek the reference documentation for explanation of the various functions.)
Thank you very much for your reply.
I use code block version 12.11
Code Block is not a compiler, it is an IDE. The compiler included in it is most likely GCC. GCC defaults to older standard, but has option -std=c++11 to enable new standard. Hopefully the IDE has means to pass that easily.
Topic archived. No new replies allowed.