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

