求素數(shù),用vector存儲的實現(xiàn)方法
更新時間:2013年05月29日 11:26:11 作者:
本篇文章是對求素數(shù),用vector存儲的實現(xiàn)方法進行了詳細(xì)的分析介紹,需要的朋友參考下
PS:如有不足之處,還望指正!
// tentotwo.cpp : 定義控制臺應(yīng)用程序的入口點。
#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;
}
復(fù)制代碼 代碼如下:
// tentotwo.cpp : 定義控制臺應(yīng)用程序的入口點。
#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;
}
相關(guān)文章
C語言實現(xiàn)可增容動態(tài)通訊錄詳細(xì)過程
這篇文章主要為大家介紹了C語言實現(xiàn)簡易通訊錄的完整流程,此通訊錄還可以增容,并且每個環(huán)節(jié)都有完整代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-05-05
排列和組合算法的實現(xiàn)方法_C語言經(jīng)典案例
下面小編就為大家?guī)硪黄帕泻徒M合算法的實現(xiàn)方法_C語言經(jīng)典案例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
C語言編程之三個方法實現(xiàn)strlen函數(shù)
本篇文章是C語言編程篇,主要為大家介紹C語言編程中實現(xiàn)strlen函數(shù)的三個方法講解,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09

