I am currently creating a program that initializes a int Vector at the beginning of the program. As the program progresses, Prime numbers will be added to the Vector. Then, the program will check if the result of if the result of a given number % the first number in the Vector == 0. If not, the program will then check it against the second number. Is there a way to do this on C++ 11 and if so, how? Thank you for any time!
-
what did you try? – 463035818_is_not_an_ai Feb 14 '17 at 12:05
-
5Main language between programmers is CODE, not story. Show Your attempt. – Jacek Cz Feb 14 '17 at 12:05
-
I don't have an attempt because I don't know how to. – SteveyMcGinins Feb 14 '17 at 12:06
-
Hint for c++11: you can use lambdas and the std:find_if function. You will find similar answers about vectors, searching and C++11 also on Stack Overflow. – StormRider Feb 14 '17 at 12:08
-
1look at [std::vector](http://en.cppreference.com/w/cpp/container/vector) what exactly dont you understand? – 463035818_is_not_an_ai Feb 14 '17 at 12:08
-
I don't understand how to go through the Vector's items and select them one by one in consecutive order – SteveyMcGinins Feb 14 '17 at 12:10
-
5[Some C++ beginner books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) may be useful. – javaLover Feb 14 '17 at 12:10
-
1@SteveyMcGinins Why don't you do what you forgot to do before asking? Like revisiting a textbook, whatever... – LogicStuff Feb 14 '17 at 12:12
-
I am 100% new to C++, have only the tiniest shred of knowledge, thanks for pointing me in the right direction! – SteveyMcGinins Feb 14 '17 at 12:14
-
You downvoters think that it is easy to start in C++ ? Even after reading good textbook someone completely new to computer programming may not know where to start. First reason is the number of concepts in c++ one has to master to just write Hello World. Even Scott Meyers used this example in his book to show how complex it is. – fjardon Feb 14 '17 at 12:19
-
2@fjardon if a textbook doesnt contain a working example of iteration over a vector, then it isnt a good textbook :P – 463035818_is_not_an_ai Feb 14 '17 at 12:21
-
@StormRider i could do that since vs2005, which was far from c++11. i think, it is c++0x – Swift - Friday Pie Feb 14 '17 at 12:27
-
@tobi303 I've just checked Stroustrup's book. And even though his presentation of for loop appears early (p 241). It is in my opinion almost impossible to understand for a complete beginner. `The expression after the colon must denote a sequence (a range); that is, it must yield a value for which we can call v.begin() and v.end() or begin(v) and end(v) to obtain iterators (4.5)`. A beginner won't know what to do with that information. You must read all previous chapters to understand it. C++ requires you to understand `iterators` in chapter 4.5 before `for` loops in chapter 9. – fjardon Feb 14 '17 at 12:35
-
@fjardon of course you must read previous chapters...you dont learn coding in a day. Question should show a minimum of effort. For example picking an example from a book and asking "What is the meaning of `begin()` and `end()`? It wasnt covered in the chapters I read so far" would be something. – 463035818_is_not_an_ai Feb 14 '17 at 12:58
-
@fjardon I certainly don't think it's easy to start in C++. I also don't think posting questions on Stack Exchange is the right way to start. – Martin Bonner supports Monica Feb 14 '17 at 13:13
2 Answers
First, you will need to push your prime numbers in the vector. At the beginning, unless you ask otherwise, the vector is empty. You can push a prime number using this snippet:
// somewhere you declare your vector
std::vector<unsigned long long> primes;
// code computing a prime number
unsigned long long prime;
....
// Push the new prime number at the end of the vector
primes.push_back(prime);
Then you will need to loop through all the prime numbers in the vector. You can use a for
loop:
for(auto prime : primes) {
. . . // code using the prime number
}
As suggested by comments, you may want to use this for
loop in order to test the primality of a number. You can easily do it with the for
loop:
unsigned long long number = ...; // the number to test
bool is_prime = true;
for(auto prime: primes) {
bool is_divisible = (0 == number % prime)
is_prime = is_prime && !is_divisible;
if(! is_prime)
break;
}
// then push it in primes if it is prime
if(is_prime)
primes.push_back(number);
Please follow the link provided by @tobi303 which is a very good resource on C++.
C++ is not easy for beginners, but if you hang on it is rewarding.

- 7,921
- 22
- 31
-
-
I understand the question like this: Test a given number against all numbers in the vector if it cannot diveded by any of the numbers in the vector then it is prime and should be added to the vector. Well the last part is not explicitly mentioned, but if that is the case, then a ranged based for loop wont help – 463035818_is_not_an_ai Feb 14 '17 at 12:24
-
1@Javalover that you can develop application that isnt dependant on 3rd party software and is possibly portable? (C# is not portable, java... kinda is , but not portable from version to version).RUST is comparable, but support of platforms is lacking. C++ got open standard, a program compliant to it is guaranteed to work on _any hardware_, provided the compiler follows standard – Swift - Friday Pie Feb 14 '17 at 12:26
-
Here is a short (c++11) code example with lambdas and std::find_if, which will show you how to find even numbers in a vector. You can use similar techniques to find odd numbers, prime numbers, etc.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> myVector;
myVector.emplace_back(5);
myVector.emplace_back(3);
myVector.emplace_back(4);
std::vector<int>::iterator it = std::find_if(myVector.begin(), myVector.end(), [](int const& elem)
{ return elem % 2 == 0; });
if (it != myVector.end())
std::cout << "My even number is: " << (*it) << std::endl;
else
std::cout << "No even numbers in vector!"<< std::endl;
system("PAUSE");
return 0;
}

- 42
- 7