-3

Try to store prime numbers and output them but the console is empty and nothing happens

enter image description here

int main() {
    vector <int> v;
    int n = 1000;
    
    int order; // Nth order
    //cin >> order;
    primes(n, v);
    for (auto i = v.begin(); i != v.end(); ++i)
        cout << *i << ' ';
    return 0;
}
Regid
  • 73
  • 1
  • 7
  • 3
    Please post your code as text, not as a screenshot. – JohnFilleau Nov 19 '20 at 16:06
  • 1
    All questions here should have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. Please [edit] this question, removing and replacing all links and images with all relevant information as plain text. All code must meet all requirements of a [mre]. You can find many other questions here that explain everything in plain text, please use them as an example for how your question should look. – Sam Varshavchik Nov 19 '20 at 16:07
  • Nothing happens because the vector v is empty in main. You pass it by value to the function primes which makes a copy and operates on the copy and not the original. Use pass by reference instead. – drescherjm Nov 19 '20 at 16:11

1 Answers1

5

You vector argument to the function is passed by value (copied). And so changes to it are not visible outside the function. Try passing it by reference.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21