Find the prime number using a vector to store the implementation of the method

  • 2020-04-02 00:59:09
  • OfStack

PS: if there are shortcomings, also hope to point out!

//Tento2.cpp: defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
void GetPrimer(int n, vector<int>& vet)
{
 for (int i = 2; i <= n; i++)
 {
  vet.push_back(i);
 }
 vector<int>::iterator ite = vet.begin();
 while (ite != vet.end())
 {
  vector<int>::iterator tmpite = ite + 1;
  while (tmpite != vet.end())
  {
   if ((*tmpite)%(*ite) == 0)
   {
    tmpite = vet.erase(tmpite);
   }
   else
   {
    tmpite ++;
   }
  }  
  ite ++;
 } 
}
int _tmain(int argc, _TCHAR* argv[])
{
 vector<int> vet;
 GetPrimer(100, vet);
 vector<int>::iterator ite = vet.begin();
 while (ite != vet.end())
 {
  cout << *ite << " ";
  ite ++;
 }
 cout << endl;
 return 0;
}


Related articles: