Need a quick hand

OK. I am trying to implement a method that removes all the prime numbers from a vector. They are all numbers from 0 to num, where num in given by the user.

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

void
findVectorPrimes(int num, set<int>& primes)
{
  
  int squareRoot = sqrt(num);
  vector <int> v;
  vector<int>::iterator m = v.begin();

   while (*m <= squareRoot)
    {

		for (int k = *m; k <= num; k++)
		{
    
			v.erase( (*m) * (k));
     
		}
	
     ++m;
  
  }
	
}


There is a separate method that fills the vector.

Its giving me an error and I can't seem to figure it out. Here is the error...

seive.cc:121:35: error: no match for âoperator*â in âv.std::vector<_Tp, _Alloc>::begin<int, std::allocator<int> >().__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator+<int*, std::vector<int> >((* &((__gnu_cxx::__normal_iterator<int*, std::vector<int> >::difference_type)m.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator*<int*, std::vector<int> >()))) * kâ

I can't make heads or tails of this. Any assistance would be great. Thanks.
Last edited on
erase expects an iterator as argument but you pass an int.
How would I be able to walk through the vector and remove every number that isn't prime? Could I just use "v.erase(v.at( ( ( *m) * (*k) ) );"?





no, at() returns the reference not an iterator.

better use list and remove():
http://www.cplusplus.com/reference/list/list/remove/


or if you still want the vector, use find() to get the iterator:
http://www.cplusplus.com/reference/algorithm/find/?kw=find
Its a sieve of arthneus problem, though. I need to find and remove the non-prime spots up to num. Not just remove any number that I would like. :/
Last edited on
why do you use the vector anyway?

primes as a set can erase the calculated number
Make it two pass
First mark all the elements to be eliminated (by instance set them to -1)
Then use http://www.cplusplus.com/reference/algorithm/remove/


> removes all the prime numbers from a vector
wait... ¿wtf are you doing?
¿what do you use the `set' for?
the vector is empty, ¿what are you trying to erase?
¿what's the purpose of the loop in line 13?
Topic archived. No new replies allowed.