求素數(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)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)百度坐標(biāo)(BD09)及GCJ02與WGS84之間的轉(zhuǎn)換的方法,文中的示例代碼講解詳細(xì),希望對大家有所幫助2023-03-03C/C++?Qt?TreeWidget?單層樹形組件應(yīng)用小結(jié)
TreeWidget?目錄樹組件,該組件適用于創(chuàng)建和管理目錄樹結(jié)構(gòu),在開發(fā)中我們經(jīng)常會把它當(dāng)作一個升級版的ListView組件使用,本文將通過TreeWidget實現(xiàn)多字段顯示,并增加一個自定義菜單,通過在指定記錄上右鍵可彈出該菜單并對指定記錄進行操作2021-11-11