1

I'm creating a program that puts prime numbers into a vector. I'll give an example to better demonstrate how this program should work:

User: 3

Output 3

User: 13

Ouput: 3 5 7 11 13

To put it in words, this program is adding up the prime numbers less than or equal to the input of the user. Then finally giving a bool of true or false if the actual input of the user was a prime number.

 if (found) { vector_output.push_back(j); } if (number == j) { if (found ==false) return false; else void checkprime::vector_finder() { for (int k=0; k < vector_output.size(); k ++) { cout << vector_output[k]; } } 

Unfortunately, my output is:

User 3

Output 3

User 13

Output: 3 5 5 5 7 7 7 7 7 9 11 13

How can I get it to where the vector does not duplicate numbers?

I thought something like an if statement right before the vector_output.pushback(j) such as

if (vector_output.size() != 0 && vector.output.back() != j) vector_output.push_back(j); 

would work, but it's not outputting anything with this.

5
  • 1
    Unless you're intentionally making it as slow as possible, you really want to use the Sieve of Eratosthenes for this. Commented Sep 30, 2013 at 0:01
  • 2
    I'd like to be to stop my program from creating duplicates then I'll move on to efficiency. I'm still very much a beginner and I realize this may not be the best way to do things. Commented Sep 30, 2013 at 0:04
  • Just as a note on the topic - determining if a number is prime is a VERY hard task. You better be praying that people enter in small numbers. Commented Sep 30, 2013 at 0:07
  • @Mdjon26: When written (even close to) correctly, the sieve won't produce duplicates in the first place. Commented Sep 30, 2013 at 0:17
  • @Dgrin91: Given that it's being entered as an int, the task isn't difficult at all. Factoring becomes difficult for large numbers, but in a typical implementation that will only accept a dozen (or fewer) digits in an int, the task isn't all that tough. Commented Sep 30, 2013 at 0:20

1 Answer 1

1

You can remove the duplicates using std::unique. For example:

std::vector<int> v{1, 2, 2, 3, 5, 9, 1}; std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); 
Sign up to request clarification or add additional context in comments.

8 Comments

@Mdjon26 After you're done calculating the primes, do std::unqiue(vector_output.begin(), vector_output.end()); and that should remove all duplicates in that range. This requires #include <algorithm>
I put it inside my void statement before I cout my vector and when I put in the number 13. it's outputting 3 5 7 9 11 13 7 7 7 9 11 13, which is really close, but for some reason it's repeating.
By itself, std::unique only squeezes the unique elements together. You still need to erase to make the container smaller.
How exactly would I erase them? I've never used the erase feature either :/. Seems like there should be an easier way to do this.
@Mdjon26 Please see the update. The only difference should be the variable names.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.