#include <iostream>
#include <vector>
#include <list>
using namespace std;
template<typename T1> void show(T1 beg, T1 end);
int main(int argc, const char *argv[])
{
int ia[] = {0, 1, 1, 2, 3, 5,8, 13, 21, 55, 89};
vector<int> ivec(ia, ia + 11);
list<int> ilist(ia, ia + 11);
show(ilist.begin(), ilist.end());
for (list<int>::iterator lit = ilist.begin();
lit != ilist.end(); ++lit)
{
if (*lit % 2 != 0)
{
lit = ilist.erase(lit);
--lit;
}
}
show(ilist.begin(), ilist.end());
show(ivec.begin(), ivec.end());
for (vector<int>::iterator vit = ivec.begin();
vit != ivec.end(); ++vit)
{
if (*vit % 2 == 0)
{
vit = ivec.erase(vit);
--vit; //理论上这里并有错,因为为我们并没有对这个无效的地方解引用。VC6.0和g++都没问题,VS2008安全检查太强了
//当然我们可以避免这样的写法,采用安全的写法,见下面程序的改写
}
}
show(ivec.begin(), ivec.end());
return 0;
}
/
template<typename T1>
void show(T1 beg, T1 end)
{
T1 tmp = end;
--tmp;
while (beg != end)
{
if (beg != tmp)
cout << *beg++ << " ";
else
cout << *beg++ << endl;
}
}
/
#include <iostream>
#include <vector>
#include <list>
using namespace std;
template<typename T1> void show(T1 beg, T1 end);
int main(int argc, const char *argv[])
{
int ia[] = {0, 1, 1, 2, 3, 5,8, 13, 21, 55, 89};
vector<int> ivec(ia, ia + 11);
list<int> ilist(ia, ia + 11);
show(ilist.begin(), ilist.end());
list<int>::iterator lit = ilist.begin();
while (lit != ilist.end())
{
if (*lit % 2 != 0)
{
lit = ilist.erase(lit);
}
else
++lit;
}
show(ilist.begin(), ilist.end());
show(ivec.begin(), ivec.end());
vector<int>::iterator vit = ivec.begin();
while (vit != ivec.end())
{
if (*vit % 2 == 0)
{
vit = ivec.erase(vit);
}
else
++vit;
}
show(ivec.begin(), ivec.end());
return 0;
}
/
template<typename T1>
void show(T1 beg, T1 end)
{
T1 tmp = end;
--tmp;
while (beg != end)
{
if (beg != tmp)
cout << *beg++ << " ";
else
cout << *beg++ << endl;
}
}
/